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.

JPA and readonly transactions when using mysql replication driverPage Title Module

JPA and readonly transactions when using mysql replication driver

If connection.isReadOnly() is true then MySQL's ReplicationDriver uses one of the slave databases, otherwise it uses the master database. To this end, I have set the @Transactional(readOnly=true) annotation whenever I'm doing queries.

Unfortunately, the underlying JpaTransactionManager or HibernateJpaDialect classes never set con.setReadOnly(true) when the readOnly setting is set to true. Here's the code:
@Override
public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
throws PersistenceException, SQLException {

As you can see, the readOnly boolean input never gets used. This differs to say the HibernateTransactionManager that calls DataSourceUtils.prepareConnectionForTransaction() which in turn calls con.setReadOnly(true).

My question is why is it that when using Jpa the con.setReadOnly(true) isn't being set if readOnly has been set to true in the @Transactional annotation?

Comment

Although I see there has been many views on this thread (over 200), it hasn't received any replies. Is there nobody on this forum that can help ascertain why read-only transactions are 'ignored' when using JPA, and if my custom HibernateJpaDialect workaround is sound?

Comment

I am running into the same problem (with Spring 3.1, wherein HibernateJpaDialect is still swallowing readOnly flag in getJdbcConnection. Also do not understand why this is the case - other than, perhaps, devs simply didn't get to this.

As far as your workaround above, I don't think it's quite right. The whole point of a ConnectionHandle is to possibly defer taking out a connection (via getConnection()). You're calling it eagerly - to set readOnly flag. Instead, I think one'd have to:
1) extend ConnectionHandle to store state of 'readOnly' flag
2) extend ConnectionHandle.getConnection to also call cxn.setReadOnly
3) possibly revert readOnly cxn setting in CH.releaseConnection - not sure

I'll give the above a shot and follow up here. Did you by chance learn anything new on this topic since your last post?