HHH-10896 - Exception thrown when dropping schema with a managed connection

This commit is contained in:
Steve Ebersole 2016-08-02 13:08:12 -05:00
parent 1661af2d8f
commit da49122113
3 changed files with 72 additions and 1 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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(