mjbennetts
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:
The sessionFactory is injected and is an instance of org.springframework.orm.hi bernate3.L ocalSessio nFactoryBe an.
I have a JUnit testcase annotated with:
And the testcase method, testSaveOrUpdate(), has an @Transactional annotation.
When I run the testcase I get the following error:
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
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);
}
The sessionFactory is injected and is an instance of org.springframework.orm.hi
I have a JUnit testcase annotated with:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@TransactionConfiguration(transactionManager="txManager", defaultRollback=false)
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
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
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.hiber nate3.Loca lSessionFa ctoryBean" >
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">${ hibernate. dialect}</ prop>
<prop key="hibernate.hbm2ddl.aut o">create< /prop>
<prop key="hibernate.order_updat es">true</ prop>
<prop key="hibernate.format_sql" >true</pro p>
<prop key="hibernate.show_sql">t rue</prop>
<prop key="hibernate.generate_st atistics"> true</prop >
<prop key="hibernate.use_sql_com ments">tru e</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>...</value>
</list>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sessionFactory"
class="org.springframework
<property name="hibernateProperties"
<props>
<prop key="hibernate.dialect">${
<prop key="hibernate.hbm2ddl.aut
<prop key="hibernate.order_updat
<prop key="hibernate.format_sql"
<prop key="hibernate.show_sql">t
<prop key="hibernate.generate_st
<prop key="hibernate.use_sql_com
</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 ..
replcae that transcation and check using hibernateTemplate whether its its working or not ..
ASKER
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.hiber nate3.Hibe rnateTrans actionMana ger">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
The "txManager" is configured in the same Spring application context xml file like this:
<bean id="txManager" class="org.springframework
<property name="sessionFactory" ref="sessionFactory" />
</bean>
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:**/applicationCo ntext.xml like this .. in ur configer
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:**/applicationCo
ASKER
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.getCurrentS ession().”
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
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
ASKER
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
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
ASKER
Hi dravidnsr,
Ok, using HibernateTemplate and getHibernateTemplate().sav eOrUpdate( entity) results in an exception: org.h2.jdbc.JdbcSQLExcepti on: Timeout trying to lock table …
If I remove the @Transactional annotation from the testcase method it works.
Cheers,
Matt
Ok, using HibernateTemplate and getHibernateTemplate().sav
If I remove the @Transactional annotation from the testcase method it works.
Cheers,
Matt
@RunWith(SpringJUnit4Class Runner.cla ss)
@ContextConfiguration(loca tions = { "classpath:/application-context.xml" }) @Transactional
@TransactionConfiguration( transactio nManager=" txManager" , defaultRollback=false)
Change like this ..
@ContextConfiguration(loca
@TransactionConfiguration(
Change like this ..
@RunWith(SpringJUnit4Class
@ContextConfiguration(loca
@Transactional
@TransactionConfiguration(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Gd...... :-)
eg:
SessionFactory sessionFactory = new Configuration().configure("oracle.cfg.xml")