Browse Source

Explicitly release rolled-back database savepoints during (long-running) transaction

Issue: SPR-12228
pull/650/head
Juergen Hoeller 10 years ago
parent
commit
9c8f7d9246
  1. 12
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/JdbcTransactionObjectSupport.java
  2. 14
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java

12
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/JdbcTransactionObjectSupport.java

@ -120,12 +120,19 @@ public abstract class JdbcTransactionObjectSupport implements SavepointManager, @@ -120,12 +120,19 @@ public abstract class JdbcTransactionObjectSupport implements SavepointManager,
*/
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
try {
getConnectionHolderForSavepoint().getConnection().rollback((Savepoint) savepoint);
conHolder.getConnection().rollback((Savepoint) savepoint);
}
catch (Throwable ex) {
throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
}
try {
conHolder.getConnection().releaseSavepoint((Savepoint) savepoint);
}
catch (Throwable ex) {
logger.debug("Could not explicitly release JDBC savepoint after rollback", ex);
}
}
/**
@ -134,8 +141,9 @@ public abstract class JdbcTransactionObjectSupport implements SavepointManager, @@ -134,8 +141,9 @@ public abstract class JdbcTransactionObjectSupport implements SavepointManager,
*/
@Override
public void releaseSavepoint(Object savepoint) throws TransactionException {
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
try {
getConnectionHolderForSavepoint().getConnection().releaseSavepoint((Savepoint) savepoint);
conHolder.getConnection().releaseSavepoint((Savepoint) savepoint);
}
catch (Throwable ex) {
logger.debug("Could not explicitly release JDBC savepoint", ex);

14
spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,13 +21,13 @@ import java.sql.DatabaseMetaData; @@ -21,13 +21,13 @@ import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
@ -57,9 +57,12 @@ import static org.mockito.BDDMockito.*; @@ -57,9 +57,12 @@ import static org.mockito.BDDMockito.*;
public class DataSourceTransactionManagerTests {
private Connection con;
private DataSource ds;
private DataSourceTransactionManager tm;
@Before
public void setUp() throws Exception {
con = mock(Connection.class);
@ -76,6 +79,7 @@ public class DataSourceTransactionManagerTests { @@ -76,6 +79,7 @@ public class DataSourceTransactionManagerTests {
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}
@Test
public void testTransactionCommitWithAutoCommitTrue() throws Exception {
doTestTransactionCommitRestoringAutoCommit(true, false, false);
@ -1290,6 +1294,7 @@ public class DataSourceTransactionManagerTests { @@ -1290,6 +1294,7 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback(sp);
verify(con).releaseSavepoint(sp);
verify(con).commit();
verify(con).isReadOnly();
verify(con).close();
@ -1395,14 +1400,19 @@ public class DataSourceTransactionManagerTests { @@ -1395,14 +1400,19 @@ public class DataSourceTransactionManagerTests {
verify(con).close();
}
private static class TestTransactionSynchronization implements TransactionSynchronization {
private DataSource dataSource;
private int status;
public boolean beforeCommitCalled;
public boolean beforeCompletionCalled;
public boolean afterCommitCalled;
public boolean afterCompletionCalled;
public TestTransactionSynchronization(DataSource dataSource, int status) {

Loading…
Cancel
Save