This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

How to share a dataSource between OJB DAO and JDBC DAOPage Title Module

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).

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.