HHH-4561 - Deprecate openConnection()/closeConnection() methods on Batcher interface

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17955 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-11-11 05:58:42 +00:00
parent f5384ba355
commit 1d3ceafffb
2 changed files with 148 additions and 91 deletions

View File

@ -28,12 +28,15 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import javax.transaction.Transaction; import javax.transaction.Transaction;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import javax.transaction.SystemException;
import javax.transaction.NotSupportedException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper; import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.exception.SQLExceptionConverter;
/** /**
* Class which provides the isolation semantics required by * Class which provides the isolation semantics required by
@ -108,66 +111,29 @@ public class Isolater {
public void delegateWork(IsolatedWork work, boolean transacted) throws HibernateException { public void delegateWork(IsolatedWork work, boolean transacted) throws HibernateException {
TransactionManager transactionManager = session.getFactory().getTransactionManager(); TransactionManager transactionManager = session.getFactory().getTransactionManager();
Transaction surroundingTransaction = null;
Connection connection = null;
boolean caughtException = false;
try { try {
// First we need to suspend any current JTA transaction and obtain // First we suspend any current JTA transaction
// a JDBC connection Transaction surroundingTransaction = transactionManager.suspend();
surroundingTransaction = transactionManager.suspend();
if ( log.isDebugEnabled() ) { if ( log.isDebugEnabled() ) {
log.debug( "surrounding JTA transaction suspended [" + surroundingTransaction + "]" ); log.debug( "surrounding JTA transaction suspended [" + surroundingTransaction + "]" );
} }
if ( transacted ) { boolean hadProblems = false;
transactionManager.begin();
}
connection = session.getBatcher().openConnection();
// doAfterTransactionCompletion the actual work
work.doWork( connection );
// if everything went ok, commit the transaction and close the obtained
// connection handle...
session.getBatcher().closeConnection( connection );
if ( transacted ) {
transactionManager.commit();
}
}
catch( Throwable t ) {
// at some point the processing went bad, so we need to:
// 1) make sure the connection handle gets released
// 2) try to cleanup the JTA context as much as possible
caughtException = true;
try { try {
if ( connection != null && !connection.isClosed() ) { // then peform the requested work
session.getBatcher().closeConnection( connection );
}
}
catch( Throwable ignore ) {
log.trace( "unable to release connection on exception [" + ignore + "]" );
}
if ( transacted ) { if ( transacted ) {
try { doTheWorkInNewTransaction( work, transactionManager );
transactionManager.rollback();
}
catch( Throwable ignore ) {
log.trace( "unable to rollback new transaction on exception [" + ignore + "]" );
}
}
// finally handle the exception
if ( t instanceof HibernateException ) {
throw ( HibernateException ) t;
} }
else { else {
throw new HibernateException( "error performing isolated work", t ); doTheWorkInNoTransaction( work );
} }
} }
catch ( HibernateException e ) {
hadProblems = true;
throw e;
}
finally { finally {
if ( surroundingTransaction != null ) {
try { try {
transactionManager.resume( surroundingTransaction ); transactionManager.resume( surroundingTransaction );
if ( log.isDebugEnabled() ) { if ( log.isDebugEnabled() ) {
@ -175,12 +141,85 @@ public class Isolater {
} }
} }
catch( Throwable t ) { catch( Throwable t ) {
if ( !caughtException ) { // if the actually work had an error use that, otherwise error based on t
throw new HibernateException( "unable to resume previously suspended transaction", t ); if ( !hadProblems ) {
//noinspection ThrowFromFinallyBlock
throw new HibernateException( "Unable to resume previously suspended transaction", t );
} }
} }
} }
} }
catch ( SystemException e ) {
throw new HibernateException( "Unable to suspend current JTA transaction", e );
}
}
private void doTheWorkInNewTransaction(IsolatedWork work, TransactionManager transactionManager) {
try {
// start the new isolated transaction
transactionManager.begin();
try {
doTheWork( work );
// if everythign went ok, commit the isolated transaction
transactionManager.commit();
}
catch ( Exception e ) {
try {
transactionManager.rollback();
}
catch ( Exception ignore ) {
log.info( "Unable to rollback isolated transaction on error [" + e + "] : [" + ignore + "]" );
}
}
}
catch ( SystemException e ) {
throw new HibernateException( "Unable to start isolated transaction", e );
}
catch ( NotSupportedException e ) {
throw new HibernateException( "Unable to start isolated transaction", e );
}
}
private void doTheWorkInNoTransaction(IsolatedWork work) {
doTheWork( work );
}
private void doTheWork(IsolatedWork work) {
try {
// obtain our isolated connection
Connection connection = session.getFactory().getConnectionProvider().getConnection();
try {
// do the actual work
work.doWork( connection );
}
catch ( HibernateException e ) {
throw e;
}
catch ( Exception e ) {
throw new HibernateException( "Unable to perform isolated work", e );
}
finally {
try {
// no matter what, release the connection (handle)
session.getFactory().getConnectionProvider().closeConnection( connection );
}
catch ( Throwable ignore ) {
log.info( "Unable to release isolated connection [" + ignore + "]" );
}
}
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
sqlExceptionConverter(),
sqle,
"unable to obtain isolated JDBC connection"
);
}
}
private SQLExceptionConverter sqlExceptionConverter() {
return session.getFactory().getSQLExceptionConverter();
} }
} }
@ -196,11 +235,10 @@ public class Isolater {
} }
public void delegateWork(IsolatedWork work, boolean transacted) throws HibernateException { public void delegateWork(IsolatedWork work, boolean transacted) throws HibernateException {
Connection connection = null;
boolean wasAutoCommit = false; boolean wasAutoCommit = false;
try { try {
connection = session.getBatcher().openConnection(); Connection connection = session.getFactory().getConnectionProvider().getConnection();
try {
if ( transacted ) { if ( transacted ) {
if ( connection.getAutoCommit() ) { if ( connection.getAutoCommit() ) {
wasAutoCommit = true; wasAutoCommit = true;
@ -214,43 +252,58 @@ public class Isolater {
connection.commit(); connection.commit();
} }
} }
catch( Throwable t ) { catch( Exception e ) {
try { try {
if ( transacted && connection != null && !connection.isClosed() ) { if ( transacted && !connection.isClosed() ) {
connection.rollback(); connection.rollback();
} }
} }
catch( Throwable ignore ) { catch( Exception ignore ) {
log.trace( "unable to release connection on exception [" + ignore + "]" ); log.info( "unable to rollback connection on exception [" + ignore + "]" );
} }
if ( t instanceof HibernateException ) { if ( e instanceof HibernateException ) {
throw ( HibernateException ) t; throw ( HibernateException ) e;
} }
else if ( t instanceof SQLException ) { else if ( e instanceof SQLException ) {
throw JDBCExceptionHelper.convert( throw JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(), sqlExceptionConverter(),
( SQLException ) t, ( SQLException ) e,
"error performing isolated work" "error performing isolated work"
); );
} }
else { else {
throw new HibernateException( "error performing isolated work", t ); throw new HibernateException( "error performing isolated work", e );
} }
} }
finally { finally {
if ( connection != null ) {
if ( transacted && wasAutoCommit ) { if ( transacted && wasAutoCommit ) {
try { try {
connection.setAutoCommit( true ); connection.setAutoCommit( true );
} }
catch( Throwable ignore ) { catch( Exception ignore ) {
log.trace( "was unable to reset connection back to auto-commit" ); log.trace( "was unable to reset connection back to auto-commit" );
} }
} }
session.getBatcher().closeConnection( connection ); try {
session.getFactory().getConnectionProvider().closeConnection( connection );
}
catch ( Exception ignore ) {
log.info( "Unable to release isolated connection [" + ignore + "]" );
} }
} }
} }
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
sqlExceptionConverter(),
sqle,
"unable to obtain isolated JDBC connection"
);
}
}
private SQLExceptionConverter sqlExceptionConverter() {
return session.getFactory().getSQLExceptionConverter();
}
} }
} }

View File

@ -165,10 +165,14 @@ public interface Batcher {
/** /**
* Obtain a JDBC connection * Obtain a JDBC connection
*
* @deprecated Obtain connections from {@link ConnectionProvider} instead
*/ */
public Connection openConnection() throws HibernateException; public Connection openConnection() throws HibernateException;
/** /**
* Dispose of the JDBC connection * Dispose of the JDBC connection
*
* @deprecated Obtain connections from {@link ConnectionProvider} instead
*/ */
public void closeConnection(Connection conn) throws HibernateException; public void closeConnection(Connection conn) throws HibernateException;