Back Forum Reply New

AbstractTransactionalSpringContextTests and IllegalTransactionStateException

mons.dbcp.BasicDataSourcequot;gt;
lt;property name=quot;driverClassNamequot;gt;lt;valuegt;${myservices.jdbc.driverclassname}lt;/valuegt;lt;/propertygt;
lt;property name=quot;uclquot;gt;lt;valuegt;${myservices.jdbc.ucl}lt;/valuegt;lt;/propertygt;
lt;property name=quot;passwordquot;gt;lt;valuegt;${myservices.jdbc.password}lt;/valuegt;lt;/propertygt;
lt;property name=quot;usernamequot;gt;lt;valuegt;${myservices.jdbc.username}lt;/valuegt;lt;/propertygt;
lt;/beangt;

lt;!-- The LOB handler for the Oracle BLOBs and CLOBs --gt;
lt;bean id=quot;oracleLobHandlerquot; class=quot;org..jdbc.support.lob.OracleLobHandlerquot; lazy-init=quot;truequot;gt;
lt;property name=quot;nativeJdbcExtractorquot;gt;
lt;ref local=quot;nativeJdbcExtractorquot; /gt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;nativeJdbcExtractorquot; class=quot;org..jdbc.support.nativejdbc.SimpleNativeJdbcExtractorquot; lazy-init=quot;truequot;/gt;

lt;!-- Hibernate SessionFactory --gt;
lt;bean id=quot;sessionFactoryquot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot;gt;
lt;property name=quot;dataSourcequot;gt;lt;ref bean=quot;dataSourcequot;/gt;lt;/propertygt;
lt;!-- Must references all OR mapping files. --gt;
lt;property name=quot;configLocationquot;gt;
lt;valuegt;classpath:hibernate.cfg.xmllt;/valuegt;
lt;/propertygt;
lt;property name=quot;hibernatePropertiesquot;gt;
lt;propsgt;
lt;prop key=quot;hibernate.cp3o.max_sizequot;gt;${myservices.hibernate.cp3o.max_size}lt;/propgt;
lt;prop key=quot;hibernate.cp3o.min_sizequot;gt;${myservices.hibernate.cp3o.min_size}lt;/propgt;
lt;prop key=quot;hibernate.cp3o.timeoutquot;gt;${myservices.hibernate.cp3o.timeout}lt;/propgt;
lt;prop key=quot;hibernate.dialectquot;gt;${myservices.hibernate.dialect}lt;/propgt;
lt;prop key=quot;hibernate.show_sqlquot;gt;${myservices.hibernate.show_sql}lt;/propgt;
lt;/propsgt;
lt;/propertygt;
lt;property name=quot;lobHandlerquot;gt;lt;ref local=quot;oracleLobHandlerquot;/gt;lt;/propertygt;
lt;/beangt;

lt;bean id=quot;transactionManagerquot; class=quot;org..orm.hibernate3.HibernateTransactionManagerquot;gt;
lt;property name=quot;sessionFactoryquot;gt;lt;ref bean=quot;sessionFactoryquot;/gt;lt;/propertygt;
lt;/beangt;

lt;bean id=quot;myServiceTargetquot; singleton=quot;falsequot; class=quot;be.isabel.pdg.myservices.service.optin.MyServicequot;gt;
lt;property name=quot;permissionDAOquot;gt;lt;ref bean=quot;permissionDAOBeanquot;/gt;lt;/propertygt;
lt;property name=quot;permissionDataquot;gt;lt;ref bean=quot;permissionDataBeanquot;/gt;lt;/propertygt;
lt;/beangt;
lt;bean id=quot;myServiceTargetSourcequot; class=quot;org..aop.target.PrototypeTargetSourcequot;gt;
lt;property name=quot;targetBeanNamequot;gt;
lt;valuegt;myServiceTargetlt;/valuegt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;myServiceTxquot; class=quot;org..transaction.interceptor.TransactionProxyFactoryBeanquot;gt;
lt;property name=quot;transactionManagerquot;gt;lt;ref bean=quot;transactionManagerquot;/gt;lt;/propertygt;
lt;property name=quot;targetquot;gt;lt;ref bean=quot;myServiceTargetSourcequot;/gt;lt;/propertygt;
lt;property name=quot;transactionAttributesquot;gt;       lt;propsgt;    lt;prop key=quot;registerOptInquot;gtROPAGATION_REQUIREDlt;/propgt;       lt;/propsgt;
lt;/propertygt;
lt;property name=quot;preInterceptorsquot;gt;
lt;listgt;
lt;ref local=quot;throwsAdvicequot;/gt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;
The transactionManager field of AbstractTransactionalSpringContextTests returns instance of the same HibernateTransactionManager class that I use in my application context but it seems that my proxy bean is injected with another instance of the same class.  

I resolved the problem by calling setTransactionManager() in onSetUpBeforeTransaction() passing as a parameter the transactionManager bean that I got from my application context.

I wonder whether there is more elegant solution than programmatically setting the transactionManager field.


Originally Posted by amelumadI resolved the problem by calling setTransactionManager() in onSetUpBeforeTransaction() passing as a parameter the transactionManager bean that I got from my application context.

I am encountering the same problem. Could you please post your solution in detail?

Thanks a lot.

The problem was (as I see it) the AbstractTransactionalSpringContextTests and my application were not using the same Spring context factory hence the transactionManager beans would be different as well.

The solution is to set the transactionManager property of AbstractTransactionalSpringContextTests with the bean obtained from my application's context.  I.e. I added the following line in my test class's onSetUpBeforeTransaction() method:

setTransactionManager((PlatformTransactionManager)  MyAppContext.getBean(quot;transactionManagerquot;));

Hope this helps.
¥
Back Forum Reply New