Hi all.
I'm doing a simple transaction test case, the objective is
1. Save two objects, one simple, one complex
2. Before committing the transaction, hit rollback
3. Test these objects are not really saved into the DB
Well, this is simple but doesn't work in my environment. Here is the trace and code, i'm sure i'm doing something wrong :(((
Test method
Code:
public void testTransaction() throws HibernateException, InterruptedException {
HibernateUtil.beginTransaction();
Session s = HibernateUtil.getSession();
VersionedContent vc = new VersionedContent();
Content published = new Content();
Content draft = new Content();
vc.setDraft(draft);
vc.setPublished(published);
String author = "transactionTest";
vc.setPublishedBy(author);
draft.setAuthor(author);
published.setAuthor(author);
Thread.sleep(1000);
Role role = new Role();
role.setName(""+new java.util.Date().getTime());
// salvamos y rollback
try {
s.save(vc);
s.save(role);
ExceptionThrower.throwHibernateExteption();
} catch (HibernateException e) {
HibernateUtil.rollbackTransaction();
com.oneplanettravel.util.log.LogUtil.getDebugLogger().debug("", e);
} finally {
HibernateUtil.closeSession();
}
try {
// ahora esos objetos no deberian existir !!
HibernateUtil.beginTransaction();
s = HibernateUtil.getSession();
boolean isEmptyList = s
.createQuery("from VersionedContent as c where c.publishedBy=:author")
.setParameter("author", author)
.list()
.isEmpty();
HibernateUtil.commitTransaction();
Assert.assertEquals(true,isEmptyList);
} catch (HibernateException e) {
HibernateUtil.rollbackTransaction();
com.oneplanettravel.util.log.LogUtil.getDebugLogger().debug("", e);
} finally {
HibernateUtil.closeSession();
}
}
HibernateUtil Class relevant codeCode:
// init --------------------------------
static {
try {
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.debug("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
// methods --------------------------
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getSession()
throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
try {
log.debug("Connection Autocommit status = " + s.connection().getAutoCommit());
} catch (Throwable e) {
// TODO Auto-generated catch block
log.debug("", e);
}
threadSession.set(s);
log.debug("Session opened with hash "+s.hashCode());
log.debug("\t\t\tvvvvvv Session vvvvvv");
sessionsOpened++;
log.debug("Total opened sessions = "+sessionsOpened);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws InfrastructureException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread. Session hash "+s.hashCode());
s.close();
sessionsOpened--;
log.debug("Total opened sessions = "+sessionsOpened);
log.debug("\t\t\t^^^^^^ Session ^^^^^^");
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws InfrastructureException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread. Session hash "+getSession().hashCode());
tx = getSession().beginTransaction();
threadTransaction.set(tx);
log.debug("Transaction hash "+tx.hashCode());
log.debug("\t\t\t\t\tvvvvvv Transaction vvvvvv");
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void commitTransaction()
throws InfrastructureException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
tx.commit();
log.debug("Committing database transaction of this thread. Transaction hash "+tx.hashCode());
}
threadTransaction.set(null);
log.debug("\t\t\t\t\t^^^^^^ Transaction ^^^^^^");
} catch (HibernateException ex) {
rollbackTransaction();
throw new InfrastructureException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void rollbackTransaction()
throws InfrastructureException {
Transaction tx = (Transaction) threadTransaction.get();
log.debug("\t^^^^^^ **** Transaction rollback **** ^^^^^^");
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Trying to rollback transaction of this thread. Transaction hash "+tx.hashCode()+". Session hash "+((Session) threadSession.get()).hashCode());
tx.rollback();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
} finally {
closeSession();
}
}
Output traceCode:
2004-08-26 15:37:29,765 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:135) - Opening new Session for this thread.
2004-08-26 15:37:29,828 DEBUG [main] zzz_dev (HibernateUtil.java:144) - Connection Autocommit status = false
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:150) - Session opened with hash 29774358
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:151) - vvvvvv Session vvvvvv
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:153) - Total opened sessions = 1
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:189) - Starting new database transaction in this thread. Session hash 29774358
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:192) - Transaction hash 28708894
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:193) - vvvvvv Transaction vvvvvv
2004-08-26 15:37:30,968 DEBUG [main] zzz_dev (HibernateUtil.java:226) - ^^^^^^ **** Transaction rollback **** ^^^^^^
2004-08-26 15:37:30,968 DEBUG [main] zzz_dev (HibernateUtil.java:230) - Trying to rollback transaction of this thread. Transaction hash 28708894. Session hash 29774358
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:170) - Closing Session of this thread. Session hash 29774358
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:173) - Total opened sessions = 0
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:174) - ^^^^^^ Session ^^^^^^
2004-08-26 15:37:31,000 DEBUG [main] zzz_dev (HibernateTest.java:140) -
net.sf.hibernate.HibernateException: Test exception. Ignore it
at com.zzz.util.test.ExceptionThrower.throwHibernateExteption(ExceptionThrower.java:35)
...
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:135) - Opening new Session for this thread.
2004-08-26 15:37:31,031 DEBUG [main] zzz_dev (HibernateUtil.java:144) - Connection Autocommit status = false
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:150) - Session opened with hash 6151022
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:151) - vvvvvv Session vvvvvv
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:153) - Total opened sessions = 1
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:189) - Starting new database transaction in this thread. Session hash 6151022
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:192) - Transaction hash 32580443
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:193) - vvvvvv Transaction vvvvvv
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:210) - Committing database transaction of this thread. Transaction hash 32580443
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:213) - ^^^^^^ Transaction ^^^^^^
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:170) - Closing Session of this thread. Session hash 6151022
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:173) - Total opened sessions = 0
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:174) - ^^^^^^ Session ^^^^^^
Hibernate version: 2.1.6
Mapping documents: Not neccesary
Name and version of the database you are using:MySQL 4.0.18 max-debug
Thanks !!!!!