Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-13433 HHH-13737 (5.3) #3100

Merged
merged 4 commits into from Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3531,8 +3531,14 @@ public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) );
}
catch ( JDBCException e ) {
if ( accessTransaction().getRollbackOnly() ) {
// assume this is the similar to the WildFly / IronJacamar "feature" described under HHH-12472
if ( accessTransaction().isActive() && accessTransaction().getRollbackOnly() ) {
// Assume this is the similar to the WildFly / IronJacamar "feature" described under HHH-12472.
// Just log the exception and return null.
if ( log.isDebugEnabled() ) {
log.debug( "JDBCException was thrown for a transaction marked for rollback; " +
"this is probably due to an operation failing fast due to the " +
"transaction marked for rollback.", e );
}
return null;
}
else {
Expand Down
@@ -0,0 +1,95 @@
/*
* 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.test.exceptionhandling;

import java.sql.SQLException;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.PersistenceException;

import org.hibernate.JDBCException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;

import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Before;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@TestForIssue( jiraKey = "HHH-13737")
@RequiresDialect(H2Dialect.class)
public class NonActiveTransactionSessionFindJdbcExceptionHandlingTest extends BaseEntityManagerFunctionalTestCase {

@Test
public void testJdbcExceptionThrown() {
// delete "description" column so that a JDBCException caused by a SQLException is thrown when looking up the AnEntity
doInJPA(
this::entityManagerFactory,
entityManager -> {
entityManager.createNativeQuery( "alter table AnEntity drop column description" ).executeUpdate();
}
);

EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.find( AnEntity.class, 1 );
fail( "A PersistenceException should have been thrown." );
}
catch ( PersistenceException ex ) {
assertTrue( JDBCException.class.isInstance( ex.getCause() ) );
assertTrue( SQLException.class.isInstance( ex.getCause().getCause() ) );
}
finally {
entityManager.close();
}
}

@Before
public void setupData() {
doInJPA(
this::entityManagerFactory,
entityManager -> {
entityManager.persist( new AnEntity( 1, "description" ) );
}
);
}

@Override
@SuppressWarnings("unchecked")
protected void addMappings(Map settings) {
settings.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, true);
}

@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { AnEntity.class };
}

@Entity(name = "AnEntity")
public static class AnEntity {
@Id
private int id;
@Column(name = "description")
private String description;

AnEntity() {
}

AnEntity(int id, String description) {
this.id = id;
this.description = description;
}
}
}