I have a test that extends AbstractTransactionalDataSourceSpringContextTests, which always fails as hibernate does not use the same connection as the JDBC template.
The code under test, first uses a JDBC template to perform a query and updates the database accordingly, then a hibernate DAO loads the entities relying on these updates. No commit occurs between the two database calls, hence they both need to use the same connection (database session) for the second to see the updates.
Debugging, I use the DataSourceUtils.getConnection(ds) at various points to check the connection, this does indeed return the same connection each time proving that there is a transaction. I also use:
to check the connection from the hibernate session factory. This also returns the same connection.Code:SessionFactory sf = (SessionFactory) applicationContext.getBean("sessionFactory"); Connection c1 = DataSourceUtils.getConnection(SessionFactoryUtils.getDataSource(sf));
But when I debug right into the hibernate code the connection used is a different one, it seems that is is got straight from the datasouce, hence no updates are seen and the test fails.
Below is stack trace of where hibernate opens the connection (if it helps).
When I perform a similar test when running within the application server there is no problem. The application server uses JTS transactions.Code:LocalDataSourceConnectionProvider.getConnection() line: 81 ConnectionManager.openConnection() line: 423 ConnectionManager.getConnection() line: 144 BatchingBatcher(AbstractBatcher).prepareQueryStatement(String, boolean, ScrollMode) line: 139 CriteriaLoader(Loader).prepareQueryStatement(QueryParameters, boolean, SessionImplementor) line: 1547 CriteriaLoader(Loader).doQuery(SessionImplementor, QueryParameters, boolean) line: 673 CriteriaLoader(Loader).doQueryAndInitializeNonLazyCollections(SessionImplementor, QueryParameters, boolean) line: 236 CriteriaLoader(Loader).doList(SessionImplementor, QueryParameters) line: 2220 CriteriaLoader(Loader).listIgnoreQueryCache(SessionImplementor, QueryParameters) line: 2104 CriteriaLoader(Loader).list(SessionImplementor, QueryParameters, Set, Type[]) line: 2099 CriteriaLoader.list(SessionImplementor) line: 94 SessionImpl.list(CriteriaImpl) line: 1569 CriteriaImpl.list() line: 283 HibernateTemplate$35.doInHibernate(Session) line: 991 HibernateTemplate.execute(HibernateCallback, boolean) line: 373 HibernateTemplate.findByCriteria(DetachedCriteria, int, int) line: 981 HibOperationDAO(HibernateEntitySelector).findByQuery(PagedPVO, String, Object[]) line: 103
So I am guessing that for some reason hibernate is not getting the connection that is already associated to the transaction. But while I was debugging the code the hibernate template says there is an existing transaction (stack trace: HibernateTemplate.execute(HibernateCallback, boolean) line: 364)
I'm sure there is just some configuration of the test that I am not doing.
Can anyone help?
Cheers.


Reply With Quote