Skip to content

Commit

Permalink
Fix JPA Transaction#commit() does not throw RollbacackException
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 authored and sebersole committed May 6, 2016
1 parent 3637bd3 commit 4f1bca7
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 639 deletions.
@@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.spi;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.LockOptions;

/**
* @author Andrea Boriero
*/
public interface ExceptionConverter {
/**
* Converts the exception thrown during the transaction commit phase
*
* @param e The exception being handled
*
* @return The converted exception
*/
RuntimeException convertCommitException(RuntimeException e);

/**
* Converts a Hibernate-specific exception into a JPA-specified exception; note that the JPA sepcification makes use
* of exceptions outside its exception hierarchy, though they are all runtime exceptions.
* <p/>
*
* @param e The Hibernate excepton.
* @param lockOptions The lock options in effect at the time of exception (can be null)
*
* @return The JPA-specified exception
*/
RuntimeException convert(HibernateException e, LockOptions lockOptions);

/**
* Converts a Hibernate-specific exception into a JPA-specified exception; note that the JPA sepcification makes use
* of exceptions outside its exception hierarchy, though they are all runtime exceptions.
* <p/>
*
* @param e The Hibernate excepton.
*
* @return The JPA-specified exception
*/
RuntimeException convert(HibernateException e);

RuntimeException convert(RuntimeException e);

RuntimeException convert(RuntimeException e, LockOptions lockOptions);

JDBCException convert(SQLException e, String message);
}
Expand Up @@ -17,7 +17,6 @@
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
import javax.persistence.LockModeType; import javax.persistence.LockModeType;
import javax.persistence.PersistenceException;
import javax.persistence.StoredProcedureQuery; import javax.persistence.StoredProcedureQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete; import javax.persistence.criteria.CriteriaDelete;
Expand Down Expand Up @@ -145,41 +144,6 @@ public boolean isTransactionInProgress() {
return delegate.isTransactionInProgress(); return delegate.isTransactionInProgress();
} }


@Override
public void handlePersistenceException(PersistenceException e) {
delegate.handlePersistenceException( e );
}

@Override
public void throwPersistenceException(PersistenceException e) {
delegate.throwPersistenceException( e );
}

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
return delegate.convert( e, lockOptions );
}

@Override
public RuntimeException convert(RuntimeException e) {
return delegate.convert( e );
}

@Override
public RuntimeException convert(HibernateException e) {
return delegate.convert( e );
}

@Override
public void throwPersistenceException(HibernateException e) {
delegate.throwPersistenceException( e );
}

@Override
public PersistenceException wrapStaleStateException(StaleStateException e) {
return delegate.wrapStaleStateException( e );
}

@Override @Override
public LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object> properties) { public LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object> properties) {
return delegate.getLockRequest( lockModeType, properties ); return delegate.getLockRequest( lockModeType, properties );
Expand Down Expand Up @@ -459,6 +423,11 @@ public LoadQueryInfluencers getLoadQueryInfluencers() {
return delegate.getLoadQueryInfluencers(); return delegate.getLoadQueryInfluencers();
} }


@Override
public ExceptionConverter getExceptionConverter() {
return delegate.getExceptionConverter();
}

@Override @Override
public SessionEventListenerManager getEventListenerManager() { public SessionEventListenerManager getEventListenerManager() {
return delegate.getEventListenerManager(); return delegate.getEventListenerManager();
Expand Down
Expand Up @@ -12,18 +12,15 @@
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
import javax.persistence.PersistenceException;


import org.hibernate.CacheMode; import org.hibernate.CacheMode;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.Interceptor; import org.hibernate.Interceptor;
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode; import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults; import org.hibernate.ScrollableResults;
import org.hibernate.SharedSessionContract; import org.hibernate.SharedSessionContract;
import org.hibernate.StaleStateException;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator; import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
Expand Down Expand Up @@ -142,57 +139,6 @@ default void checkOpen() {
*/ */
void markForRollbackOnly(); void markForRollbackOnly();


/**
* Handles marking for rollback and other such operations that need to occur depending on the type of
* exception being handled.
*
* @param e The exception being handled.
*/
void handlePersistenceException(PersistenceException e);

/**
* Delegates to {@link #handlePersistenceException} and then throws the given exception.
*
* @param e The exception being handled and finally thrown.
*/
void throwPersistenceException(PersistenceException e);

/**
* Converts a Hibernate-specific exception into a JPA-specified exception; note that the JPA sepcification makes use
* of exceptions outside its exception hierarchy, though they are all runtime exceptions.
* <p/>
* Any appropriate/needed calls to {@link #handlePersistenceException} are also made.
*
* @param e The Hibernate excepton.
* @param lockOptions The lock options in effect at the time of exception (can be null)
*
* @return The JPA-specified exception
*/
RuntimeException convert(HibernateException e, LockOptions lockOptions);

/**
* Converts a Hibernate-specific exception into a JPA-specified exception; note that the JPA sepcification makes use
* of exceptions outside its exception hierarchy, though they are all runtime exceptions.
* <p/>
* Any appropriate/needed calls to {@link #handlePersistenceException} are also made.
*
* @param e The Hibernate excepton.
*
* @return The JPA-specified exception
*/
RuntimeException convert(HibernateException e);

RuntimeException convert(RuntimeException e);

/**
* Delegates to {@link #convert} and then throws the given exception.
*
* @param e The exception being handled and finally thrown.
*/
void throwPersistenceException(HibernateException e);

PersistenceException wrapStaleStateException(StaleStateException e);

/** /**
* System time beforeQuery the start of the transaction * System time beforeQuery the start of the transaction
*/ */
Expand Down Expand Up @@ -451,4 +397,6 @@ int executeNativeUpdate(NativeSQLQuerySpecification specification, QueryParamete
* should never be null. * should never be null.
*/ */
LoadQueryInfluencers getLoadQueryInfluencers(); LoadQueryInfluencers getLoadQueryInfluencers();

ExceptionConverter getExceptionConverter();
} }
Expand Up @@ -10,6 +10,7 @@


import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.TransactionException; import org.hibernate.TransactionException;
import org.hibernate.engine.spi.ExceptionConverter;
import org.hibernate.engine.transaction.spi.TransactionImplementor; import org.hibernate.engine.transaction.spi.TransactionImplementor;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.resource.transaction.spi.TransactionCoordinator; import org.hibernate.resource.transaction.spi.TransactionCoordinator;
Expand All @@ -27,10 +28,12 @@ public class TransactionImpl implements TransactionImplementor {
private static final Logger LOG = CoreLogging.logger( TransactionImpl.class ); private static final Logger LOG = CoreLogging.logger( TransactionImpl.class );


private final TransactionCoordinator transactionCoordinator; private final TransactionCoordinator transactionCoordinator;
private final ExceptionConverter exceptionConverter;
private TransactionDriver transactionDriverControl; private TransactionDriver transactionDriverControl;


public TransactionImpl(TransactionCoordinator transactionCoordinator) { public TransactionImpl(TransactionCoordinator transactionCoordinator, ExceptionConverter exceptionConverter) {
this.transactionCoordinator = transactionCoordinator; this.transactionCoordinator = transactionCoordinator;
this.exceptionConverter = exceptionConverter;
} }


@Override @Override
Expand Down Expand Up @@ -60,8 +63,12 @@ public void commit() {
} }


LOG.debug( "committing" ); LOG.debug( "committing" );

try {
internalGetTransactionDriverControl().commit(); internalGetTransactionDriverControl().commit();
}
catch (RuntimeException e) {
throw exceptionConverter.convertCommitException( e );
}
} }


public TransactionDriver internalGetTransactionDriverControl() { public TransactionDriver internalGetTransactionDriverControl() {
Expand Down

0 comments on commit 4f1bca7

Please sign in to comment.