Explicitly release rolled-back database savepoints during (long-running) transaction
Issue: SPR-12228
This commit is contained in:
@@ -120,12 +120,19 @@ public abstract class JdbcTransactionObjectSupport implements SavepointManager,
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
|
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
|
||||||
|
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
|
||||||
try {
|
try {
|
||||||
getConnectionHolderForSavepoint().getConnection().rollback((Savepoint) savepoint);
|
conHolder.getConnection().rollback((Savepoint) savepoint);
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
throw new TransactionSystemException("Could not roll back to JDBC savepoint", 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,
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void releaseSavepoint(Object savepoint) throws TransactionException {
|
public void releaseSavepoint(Object savepoint) throws TransactionException {
|
||||||
|
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
|
||||||
try {
|
try {
|
||||||
getConnectionHolderForSavepoint().getConnection().releaseSavepoint((Savepoint) savepoint);
|
conHolder.getConnection().releaseSavepoint((Savepoint) savepoint);
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
logger.debug("Could not explicitly release JDBC savepoint", ex);
|
logger.debug("Could not explicitly release JDBC savepoint", ex);
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -21,13 +21,13 @@ import java.sql.DatabaseMetaData;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Savepoint;
|
import java.sql.Savepoint;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
import org.springframework.jdbc.UncategorizedSQLException;
|
import org.springframework.jdbc.UncategorizedSQLException;
|
||||||
import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
|
import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
|
||||||
@@ -57,9 +57,12 @@ import static org.mockito.BDDMockito.*;
|
|||||||
public class DataSourceTransactionManagerTests {
|
public class DataSourceTransactionManagerTests {
|
||||||
|
|
||||||
private Connection con;
|
private Connection con;
|
||||||
|
|
||||||
private DataSource ds;
|
private DataSource ds;
|
||||||
|
|
||||||
private DataSourceTransactionManager tm;
|
private DataSourceTransactionManager tm;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
con = mock(Connection.class);
|
con = mock(Connection.class);
|
||||||
@@ -76,6 +79,7 @@ public class DataSourceTransactionManagerTests {
|
|||||||
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
|
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTransactionCommitWithAutoCommitTrue() throws Exception {
|
public void testTransactionCommitWithAutoCommitTrue() throws Exception {
|
||||||
doTestTransactionCommitRestoringAutoCommit(true, false, false);
|
doTestTransactionCommitRestoringAutoCommit(true, false, false);
|
||||||
@@ -1290,6 +1294,7 @@ public class DataSourceTransactionManagerTests {
|
|||||||
|
|
||||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||||
verify(con).rollback(sp);
|
verify(con).rollback(sp);
|
||||||
|
verify(con).releaseSavepoint(sp);
|
||||||
verify(con).commit();
|
verify(con).commit();
|
||||||
verify(con).isReadOnly();
|
verify(con).isReadOnly();
|
||||||
verify(con).close();
|
verify(con).close();
|
||||||
@@ -1395,14 +1400,19 @@ public class DataSourceTransactionManagerTests {
|
|||||||
verify(con).close();
|
verify(con).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class TestTransactionSynchronization implements TransactionSynchronization {
|
private static class TestTransactionSynchronization implements TransactionSynchronization {
|
||||||
|
|
||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
|
|
||||||
private int status;
|
private int status;
|
||||||
|
|
||||||
public boolean beforeCommitCalled;
|
public boolean beforeCommitCalled;
|
||||||
|
|
||||||
public boolean beforeCompletionCalled;
|
public boolean beforeCompletionCalled;
|
||||||
|
|
||||||
public boolean afterCommitCalled;
|
public boolean afterCommitCalled;
|
||||||
|
|
||||||
public boolean afterCompletionCalled;
|
public boolean afterCompletionCalled;
|
||||||
|
|
||||||
public TestTransactionSynchronization(DataSource dataSource, int status) {
|
public TestTransactionSynchronization(DataSource dataSource, int status) {
|
||||||
|
|||||||
Reference in New Issue
Block a user