spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

JdbcSession update fails during read-only transaction

Open kherrala opened this issue 10 years ago • 8 comments

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 avatar Mar 25 '16 21:03 kherrala

@kherrala Thanks for the report! @vpavic Can you take a look at this?

rwinch avatar Mar 25 '16 21:03 rwinch

@rwinch I'll get a look and try to reproduce later today.

vpavic avatar Mar 26 '16 06:03 vpavic

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:

true if 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.

vpavic avatar Mar 26 '16 12:03 vpavic

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#cleanUpExpiredSessions method is annotated with @Scheduled and 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 true will affect all Spring-managed beans requiring proxying, not just those marked with @Transactional. For example, other beans marked with Spring's @Async annotation 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#cleanUpExpiredSessions to 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.

vpavic avatar Mar 26 '16 22:03 vpavic

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 avatar Mar 27 '16 20:03 vpavic

@vpavic Thanks for the updates. I have commented on the prototype you linked to.

rwinch avatar Mar 30 '16 15:03 rwinch

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?

kherrala avatar Mar 31 '16 06:03 kherrala

The issue could probably be solved that way but I think it makes things more complex.

vpavic avatar Mar 31 '16 08:03 vpavic