diff --git a/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionAccessLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionAccessLogger.java new file mode 100644 index 0000000000..29b79e34a1 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionAccessLogger.java @@ -0,0 +1,44 @@ +/* + * 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.internal.log; + +import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; + +import org.jboss.logging.BasicLogger; +import org.jboss.logging.Logger; +import org.jboss.logging.annotations.LogMessage; +import org.jboss.logging.annotations.Message; +import org.jboss.logging.annotations.MessageLogger; +import org.jboss.logging.annotations.ValidIdRange; + +import static org.jboss.logging.Logger.Level.INFO; + +/** + * @author Steve Ebersole + */ +@MessageLogger( projectCode = "HHH" ) +@ValidIdRange( min = 10001501, max = 10001500 ) +public interface ConnectionAccessLogger extends BasicLogger { + String LOGGER_NAME = "org.hibernate.orm.connections.access"; + + /** + * Static access to the logging instance + */ + ConnectionAccessLogger INSTANCE = Logger.getMessageLogger( + ConnectionAccessLogger.class, + LOGGER_NAME + ); + + + @LogMessage(level = INFO) + @Message( + value = "Connection obtained from JdbcConnectionAccess [%s] for (non-JTA) DDL execution was not in auto-commit mode; " + + "the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.", + id = 10001501 + ) + void informConnectionLocalTransactionForNonJtaDdl(JdbcConnectionAccess jdbcConnectionAccess); +} diff --git a/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionPoolingLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionPoolingLogger.java index b23a5d955d..f9fd280479 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionPoolingLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/log/ConnectionPoolingLogger.java @@ -9,6 +9,8 @@ package org.hibernate.internal.log; import java.sql.SQLException; import java.util.Properties; +import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; + import org.jboss.logging.BasicLogger; import org.jboss.logging.Logger; import org.jboss.logging.annotations.Cause; @@ -31,7 +33,7 @@ public interface ConnectionPoolingLogger extends BasicLogger { */ public static final ConnectionPoolingLogger CONNECTIONS_LOGGER = Logger.getMessageLogger( ConnectionPoolingLogger.class, - "org.hibernate.orm.connections" + "org.hibernate.orm.connections.pooling" ); @LogMessage(level = INFO) diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java index bd21b9fc50..50d7bebcbf 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java @@ -9,6 +9,8 @@ package org.hibernate.resource.transaction.backend.jdbc.internal; import java.sql.Connection; import java.sql.SQLException; +import org.hibernate.internal.log.ConnectionAccessLogger; +import org.hibernate.internal.log.ConnectionPoolingLogger; import org.hibernate.resource.transaction.spi.DdlTransactionIsolator; import org.hibernate.tool.schema.internal.exec.JdbcContext; @@ -40,6 +42,29 @@ public class DdlTransactionIsolatorNonJtaImpl implements DdlTransactionIsolator if ( jdbcConnection == null ) { try { this.jdbcConnection = jdbcContext.getJdbcConnectionAccess().obtainConnection(); + + try { + if ( !jdbcConnection.getAutoCommit() ) { + ConnectionAccessLogger.INSTANCE.informConnectionLocalTransactionForNonJtaDdl( jdbcContext.getJdbcConnectionAccess() ); + + try { + jdbcConnection.commit(); + jdbcConnection.setAutoCommit( true ); + } + catch (SQLException e) { + throw jdbcContext.getSqlExceptionHelper().convert( + e, + "Unable to set JDBC Connection into auto-commit mode in preparation for DDL execution" + ); + } + } + } + catch (SQLException e) { + throw jdbcContext.getSqlExceptionHelper().convert( + e, + "Unable to check JDBC Connection auto-commit in preparation for DDL execution" + ); + } } catch (SQLException e) { throw jdbcContext.getSqlExceptionHelper().convert(