Sep 24th, 2004, 10:16 AM
How to share a dataSource between OJB DAO and JDBC DAO
In our application, we have a Service that calls OJB DAO's and JDBC DAO's. The service is wrapped with the Transaction Proxy Factory Bean and is configured so a transaction is required.
I assumed that this would mean that all DAO's called by this service would share the same dataSource. Unfortunately, they aren't. I know for a fact that the JDBC DAO and OJB DAO's don't share a connection and it looks like different OJB DAO's don't share a connection.
Since the DAO's don't share a connection, they can't be on the same transaction.
How should I configure the DAO's or the Transaction Proxy Factory Bean to make sure that all database work happens on the same database connection (and therefore the same transaction).
Sep 24th, 2004, 05:11 PM
First, make sure that your OJB DAO and your JDBC DAO definitely access the same DataSource. You could access the same JNDI DataSource to achieve this, or use Spring's LocalOjbConfigurer in combination with LocalDataSourceConnectionFactory (see Petclinic's "applicationContext-ojb.xml" and "OJB.properties" files for an example).
Then, pass the DataSource reference that your DAO is using to PersistenceBrokerTransactionManager's "dataSource" property. This will automatically expose OJB transactions as JDBC transactions too; Spring's JdbcTemplate will automatically participate in these. Alternatively, you could use JtaTransactionManager.
Sep 28th, 2004, 11:02 AM
Thanks. I set the PersistenceBrokerTransactionManager's dataSource property and that worked. That property wasn't set in the Petclinic application so I didn't know it existed.