JdbcSession update fails during read-only transaction
Flushing or closing HttpServletResponse outputStream while still inside readOnly transaction fails because JdbcOperationsRepository tries to execute SQL update query. The expection is as follows:
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [UPDATE SPRING_SESSION SET LAST_ACCESS_TIME = ? WHERE SESSION_ID = ?]; SQL state [25006]; error code [0]; ERROR: cannot execute UPDATE in a read-only transaction; nested exception is org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
Behaviour can be replicated by calling following fragment inside spring annotated transaction:
try (InputStream is = connection.getInputStream()) { IOUtils.copy(is, response.getOutputStream()); }
Default configuration should avoid sharing user defined transaction in JdbcTemplate or defer updating database session until filter chain has been processed in SessionRepositoryFilter.
@kherrala Thanks for the report! @vpavic Can you take a look at this?
@rwinch I'll get a look and try to reproduce later today.
I've created an integration test to reproduce this, it is as simple as:
@Test
@Transactional(readOnly = true)
public void savesInReadOnlyTransaction() {
JdbcOperationsSessionRepository.JdbcSession toSave = this.repository
.createSession();
this.repository.save(toSave);
}
That catch is to use PostgreSQL database, since the test passes for me on H2 and MySQL/MariaDB. I have access to an Oracle 11gR2 instance, so I'll also try there a bit later.
PostgreSQL's transaction system correctly applies the read-only optimization hint, as described in org.springframework.transaction.annotation.Transactional#readOnly javadoc:
trueif the transaction is read-only.Defaults to
false.This just serves as a hint for the actual transaction subsystem; it will not necessarily cause failure of write access attempts. A transaction manager which cannot interpret the read-only hint will not throw an exception when asked for a read-only transaction.
From previous comments it was obvious that fixing this issue requires enabling transaction management and applying Propagation.REQUIRES_NEW to JdbcOperationsSessionRepository operations that potentially persist session to database.
However, since Spring transaction management defaults to standard Java interface-based proxies, JdbcOperationsSessionRepository is problematic for proxying for two reasons:
- it implements
FindByIndexNameSessionRepository<JdbcOperationsSessionRepository.JdbcSession> - its
JdbcOperationsSessionRepository#cleanUpExpiredSessionsmethod is annotated with@Scheduledand it does not implement any interface
This can be worked around by using @EnableTransactionManagement(proxyTargetClass = true) however this is IMO not a good idea considering the org.springframework.transaction.annotation.EnableTransactionManagement#proxyTargetClass javadoc:
Indicate whether subclass-based (CGLIB) proxies are to be created (
true) as opposed to standard Java interface-based proxies (false). The default is false. Applicable only if mode() is set to AdviceMode.PROXY.Note that setting this attribute to
truewill affect all Spring-managed beans requiring proxying, not just those marked with@Transactional. For example, other beans marked with Spring's@Asyncannotation will be upgraded to subclass proxying at the same time. This approach has no negative impact in practice unless one is explicitly expecting one type of proxy vs another, e.g. in tests.
Sticking with interface-based proxies reqiures JdbcOperationsSessionRepository to:
- implement
FindByIndexNameSessionRepository<ExpiredSession> - introduce interface to allow
JdbcOperationsSessionRepository#cleanUpExpiredSessionsto be proxied
To demonstrate these two approaches I've got two prototypes in my fork:
- ~~interface-based proxies~~
- ~~subclass-based (CGLIB) proxies~~
@rwinch take a look when you can, ~~I believe we should go with interface-based proxies approach~~. I can finish this up on Monday if there are no major changes.
OK, I've managed to take a brief fresh look at this today and considering the broader perspective proxying should be avoided altogether and transactions should be handled internally using TransactionTemplate.
Here's the prototype for this approach.
@vpavic Thanks for the updates. I have commented on the prototype you linked to.
Just a quick thought but could we just use TransactionSynchronizationManager to check if we are in active transaction with readOnly mode and defer update after commit?
The issue could probably be solved that way but I think it makes things more complex.