From fc230fa0e8476d2b58ae1586e595d3686f06fd9b Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 19 Dec 2024 09:32:20 +0100 Subject: [PATCH 1/2] HHH-18961 Add test for issue --- .../txn/JtaObtainConnectionExceptionTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/txn/JtaObtainConnectionExceptionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/txn/JtaObtainConnectionExceptionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/txn/JtaObtainConnectionExceptionTest.java new file mode 100644 index 000000000000..f909dba75995 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/txn/JtaObtainConnectionExceptionTest.java @@ -0,0 +1,113 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.jpa.txn; + +import jakarta.transaction.HeuristicMixedException; +import jakarta.transaction.HeuristicRollbackException; +import jakarta.transaction.InvalidTransactionException; +import jakarta.transaction.NotSupportedException; +import jakarta.transaction.RollbackException; +import jakarta.transaction.SystemException; +import jakarta.transaction.Transaction; +import jakarta.transaction.TransactionManager; +import org.hibernate.HibernateException; +import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; +import org.hibernate.jdbc.AbstractReturningWork; +import org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; + +@JiraKey("HHH-18961") +public class JtaObtainConnectionExceptionTest { + + @Test + public void testIt() { + final JtaIsolationDelegate isolationDelegate = new JtaIsolationDelegate( + new ObtainConnectionSqlExceptionConnectionAccess(), + null, + new TransactionManagerImpl() ); + + assertThrowsExactly( HibernateException.class, () -> + isolationDelegate.delegateWork( + new AbstractReturningWork<>() { + @Override + public Object execute(Connection connection) { + return null; + } + }, + false + ) + ); + } + + public static class ObtainConnectionSqlExceptionConnectionAccess implements JdbcConnectionAccess { + @Override + public Connection obtainConnection() throws SQLException { + throw new SQLException( "No connection" ); + } + + @Override + public void releaseConnection(Connection connection) throws SQLException { + + } + + @Override + public boolean supportsAggressiveRelease() { + return false; + } + } + + public static class TransactionManagerImpl implements TransactionManager { + @Override + public void begin() throws NotSupportedException, SystemException { + + } + + @Override + public void commit() + throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { + } + + @Override + public int getStatus() throws SystemException { + return 0; + } + + @Override + public Transaction getTransaction() throws SystemException { + return null; + } + + @Override + public void resume(Transaction transaction) + throws InvalidTransactionException, IllegalStateException, SystemException { + } + + @Override + public void rollback() throws IllegalStateException, SecurityException, SystemException { + + } + + @Override + public void setRollbackOnly() throws IllegalStateException, SystemException { + + } + + @Override + public void setTransactionTimeout(int i) throws SystemException { + + } + + @Override + public Transaction suspend() throws SystemException { + return null; + } + } +} From 0468b2d9b29121d991ae93928e1e7a6cae174ece Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 19 Dec 2024 09:40:40 +0100 Subject: [PATCH 2/2] HHH-18961 JtaIsolationDelegate, obtaining connection : NPE when SQLExceptionConversionDelegate#convert returns null --- .../backend/jta/internal/JtaIsolationDelegate.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jta/internal/JtaIsolationDelegate.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jta/internal/JtaIsolationDelegate.java index aa332cee369d..53cf8921b35b 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jta/internal/JtaIsolationDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jta/internal/JtaIsolationDelegate.java @@ -203,7 +203,11 @@ private T doTheWork(WorkExecutorVisitable work) { } } catch (SQLException e) { - throw sqlExceptionConverter().apply( e, "unable to obtain isolated JDBC connection" ); + final JDBCException jdbcException = sqlExceptionConverter().apply( e, "unable to obtain isolated JDBC connection" ); + if ( jdbcException == null ) { + throw new HibernateException( "Unable to obtain isolated JDBC connection", e ); + } + throw jdbcException; } }