Link to home
Start Free TrialLog in
Avatar of mjbennetts
mjbennettsFlag for Australia

asked on

Could not commit Hibernate transaction

I'm using Spring 3.0.2.RELEASE, Hibernate 3.3.2.GA and JUnit 4.8.1

I have a simple DAO that does NOT use HibernateTemplate, that has a saveOrUpdate method that looks like this:

Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
try
{
   session.saveOrUpdate(entity);
   tx.commit();
}
catch (Exception e)
{
   tx.rollback();
   throw new DataAccessException(e.getMessage(), e);
}

Open in new window


The sessionFactory is injected and is an instance of org.springframework.orm.hibernate3.LocalSessionFactoryBean.

I have a JUnit testcase annotated with:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@TransactionConfiguration(transactionManager="txManager", defaultRollback=false)

Open in new window


And the testcase method, testSaveOrUpdate(), has an @Transactional annotation.

When I run the testcase I get the following error:
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:515)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)
	at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:426)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.TransactionException: Transaction not successfully started
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:131)
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
	... 25 more

Open in new window


I think the exception is being throw because a rollback() is being executed after my DAO commits the transaction, but it's not in my code and the defaultRollBack setting on the testcase is false. Any idea where the issue might be?

Thnaks,
Matt
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

can u show the sessionFactory xml flie
eg:
SessionFactory sessionFactory = new Configuration().configure("oracle.cfg.xml")
 
Avatar of mjbennetts

ASKER

Sure, here's the section from the Spring application context xml file (without all the Hibernate mappings):

      <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="hibernateProperties">
                  <props>
                        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                        <prop key="hibernate.hbm2ddl.auto">create</prop>
                        <prop key="hibernate.order_updates">true</prop>
                        <prop key="hibernate.format_sql">true</prop>
                        <prop key="hibernate.show_sql">true</prop>
                        <prop key="hibernate.generate_statistics">true</prop>
                        <prop key="hibernate.use_sql_comments">true</prop>
                  </props>
            </property>
            <property name="mappingResources">
                  <list>
                        <value>...</value>
                  </list>
            </property>
            <property name="dataSource" ref="dataSource" />
      </bean>

so ur not using txManager here then how ur file find ..
replcae that transcation and check using hibernateTemplate whether its its working or not ..
 
 
I'm sorry but I don't understand what you're getting at.

The "txManager" is configured in the same Spring application context xml file like this:

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
Avatar of saravanakg
saravanakg

Hi ,
   Kindly refer this forum for more info.

https://forum.hibernate.org/viewtopic.php?p=2395043
That only i need... your configer is correct only .
Change ur code for hibernatetemplate and check whether ur code is working fine or not ??
i think the pblm in ur code only .check that replace by
 hibernatetemplate .saveOrUpdate()
 
if its working fine means then
then u have mention the
classpath:**/applicationContext.xml like this .. in ur configer  
 
 
 
I will try the HibernateTemple suggestion and get back to you, but  I‘m trying to avoid coupling my DAO classes to Spring; even the API documentation for Spring recommends against the use of HibernateTemplate now days:  “NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().”

Also, I still don’t understand what you are indicating with regard to the application context.  The context is loaded and process correctly.

Cheers,
Matt
Hi saravanakg,

I've read the link you posted but it didn't tell me anything I didn't already know. I’m pretty sure I know what's going on, I just don't know how.
If you look at my original post you'll see in my code that a roll back is only performed if there's an exception, and I'm not getting any exceptions in my code. The exception occurs after my testcase has successfully completed, and Spring (I think) is trying to roll back the transaction after the testcase has completed. I have explicitly told Spring to not perform a roll back after the testcase using the @TransactionConfiguration annotation with the “defaultRollback” parameter set to false, but an additional commit or roll back is being executed after the testcase has ended.

Cheers,
Matt

Hi dravidnsr,

Ok, using HibernateTemplate and getHibernateTemplate().saveOrUpdate(entity) results in an exception: org.h2.jdbc.JdbcSQLException: Timeout trying to lock table …
If I remove the @Transactional annotation from the testcase method it works.

Cheers,
Matt
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "classpath:/application-context.xml" })  @Transactional
@TransactionConfiguration(transactionManager="txManager", defaultRollback=false)
Change like this ..


@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "classpath:/application-context.xml" })  
@Transactional
@TransactionConfiguration(transactionManager="txManager", defaultRollback=false)
ASKER CERTIFIED SOLUTION
Avatar of mjbennetts
mjbennetts
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gd...... :-)