|
From: Colin S. <col...@ex...> - 2004-12-16 15:52:42
|
Brian,
Just use HibernateTransactionManager, and make sure you use the same
DataSource both for the Hibernate LocalSessionFactoryBean, and for your
Spring-based JDBC code. Your JDBC code _will_ share the same transaction
as used for Hibernate.
The HibernateTransactionManager ensures that the connection used for
Hibernate is also bound to the current thread and synchronized to the
transaction (keyed to the datasource), and when the JDBC code tries to
use this datasource it will reuse the bound connection.
Regards,
Colin
Brian McCallister wrote:
> If I am using both hibernate and a raw datasource in an application,
> is there a mechanism for having them both work in the same transaction
> other than JTA?
>
> The LocalSessionFactoryBean is configured to use the same data source
> as I am attempting to use standalone -- but I am not sure if a
> transaction manager exists which will do DS transactions around
> hibernate transactions, etc?
>
> Here is sample test case (with manually demarcated tx's for testing)
>
> transactionManager is the HibernateTransactionManager and the
> DataSource is just a DriverManagerDataSource
>
> I have can happily throw a DataSourceTransactionManager into the mix,
> but figure best to ask about ways to do this =)
>
> public void testHibTxManagerSharesDSTxWithHib() throws Exception
> {
> final DefaultTransactionDefinition def = new
> DefaultTransactionDefinition();
>
> def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
> final PlatformTransactionManager ptm =
> (PlatformTransactionManager) getContext().getBean("transactionManager");
> final TransactionStatus status = ptm.getTransaction(def);
>
> final IDBI dbi = (IDBI) getContext().getBean("dbi");
> final Handle one = dbi.open();
>
> final SessionFactory factory = (SessionFactory)
> getContext().getBean("sessionFactory");
> final Session sesion = factory.openSession();
>
> one.execute("insert into batch_log (batch_id) values (87)");
> assertFalse(one.getConnection().getAutoCommit());
>
> final BatchLog log = (BatchLog) sesion.get(BatchLog.class, new
> Long(87));
> assertNotNull(log);
>
> ptm.commit(status);
> }
>
> Thanks!
>
> -Brian
|