HHH-8459 - Improved handling of JPA schema-generation target files

This commit is contained in:
Steve Ebersole 2013-08-27 16:16:53 -05:00
parent 939c1f2743
commit 91dace33b3
4 changed files with 164 additions and 40 deletions

View File

@ -83,6 +83,7 @@ subprojects { subProject ->
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'maven-publish-auth'
apply plugin: 'maven' // temporary, still needed for install task until bug with publishToMavenLocal task is fixed
apply plugin: 'osgi'
apply from: "../utilities.gradle"

View File

@ -112,6 +112,7 @@ public class JpaSchemaGenerator {
if ( databaseAction == SchemaGenAction.NONE && scriptsAction == SchemaGenAction.NONE ) {
// no actions needed
log.debug( "No actions specified; doing nothing" );
return;
}
@ -404,48 +405,12 @@ public class JpaSchemaGenerator {
);
if ( providedConnection != null ) {
return new JdbcConnectionContext(
new JdbcConnectionAccess() {
@Override
public Connection obtainConnection() throws SQLException {
return providedConnection;
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
// do nothing
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
},
sqlStatementLogger
);
return new JdbcConnectionContext( new ProvidedJdbcConnectionAccess( providedConnection ), sqlStatementLogger );
}
final ConnectionProvider connectionProvider = serviceRegistry.getService( ConnectionProvider.class );
if ( connectionProvider != null ) {
return new JdbcConnectionContext(
new JdbcConnectionAccess() {
@Override
public Connection obtainConnection() throws SQLException {
return connectionProvider.getConnection();
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
connectionProvider.closeConnection( connection );
}
@Override
public boolean supportsAggressiveRelease() {
return connectionProvider.supportsAggressiveRelease();
}
},
sqlStatementLogger
);
return new JdbcConnectionContext( new ConnectionProviderJdbcConnectionAccess( connectionProvider ), sqlStatementLogger );
}
// otherwise, return a no-op impl
@ -672,4 +637,147 @@ public class JpaSchemaGenerator {
}
}
/**
* Defines access to a JDBC Connection explicitly provided to us by the application
*/
private static class ProvidedJdbcConnectionAccess implements JdbcConnectionAccess {
private final Connection jdbcConnection;
private final boolean wasInitiallyAutoCommit;
private ProvidedJdbcConnectionAccess(Connection jdbcConnection) {
this.jdbcConnection = jdbcConnection;
boolean wasInitiallyAutoCommit;
try {
wasInitiallyAutoCommit = jdbcConnection.getAutoCommit();
if ( ! wasInitiallyAutoCommit ) {
try {
jdbcConnection.setAutoCommit( true );
}
catch (SQLException e) {
throw new PersistenceException(
String.format(
"Could not set provided connection [%s] to auto-commit mode" +
" (needed for schema generation)",
jdbcConnection
),
e
);
}
}
}
catch (SQLException ignore) {
wasInitiallyAutoCommit = false;
}
log.debugf( "wasInitiallyAutoCommit=%s", wasInitiallyAutoCommit );
this.wasInitiallyAutoCommit = wasInitiallyAutoCommit;
}
@Override
public Connection obtainConnection() throws SQLException {
return jdbcConnection;
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
if ( ! wasInitiallyAutoCommit ) {
try {
if ( jdbcConnection.getAutoCommit() ) {
jdbcConnection.setAutoCommit( false );
}
}
catch (SQLException e) {
log.info( "Was unable to reset JDBC connection to no longer be in auto-commit mode" );
}
}
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
}
/**
* Defines access to a JDBC Connection through the defined ConnectionProvider
*/
private static class ConnectionProviderJdbcConnectionAccess implements JdbcConnectionAccess {
private final ConnectionProvider connectionProvider;
private final Connection jdbcConnection;
private final boolean wasInitiallyAutoCommit;
private ConnectionProviderJdbcConnectionAccess(ConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
try {
this.jdbcConnection = connectionProvider.getConnection();
}
catch (SQLException e) {
throw new PersistenceException( "Unable to obtain JDBC Connection", e );
}
boolean wasInitiallyAutoCommit;
try {
wasInitiallyAutoCommit = jdbcConnection.getAutoCommit();
if ( ! wasInitiallyAutoCommit ) {
try {
jdbcConnection.setAutoCommit( true );
}
catch (SQLException e) {
throw new PersistenceException(
String.format(
"Could not set provided connection [%s] to auto-commit mode" +
" (needed for schema generation)",
jdbcConnection
),
e
);
}
}
}
catch (SQLException ignore) {
wasInitiallyAutoCommit = false;
}
log.debugf( "wasInitiallyAutoCommit=%s", wasInitiallyAutoCommit );
this.wasInitiallyAutoCommit = wasInitiallyAutoCommit;
}
@Override
public Connection obtainConnection() throws SQLException {
return jdbcConnection;
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
if ( connection != this.jdbcConnection ) {
throw new PersistenceException(
String.format(
"Connection [%s] passed back to %s was not the one obtained [%s] from it",
connection,
ConnectionProviderJdbcConnectionAccess.class.getName(),
jdbcConnection
)
);
}
if ( ! wasInitiallyAutoCommit ) {
try {
if ( jdbcConnection.getAutoCommit() ) {
jdbcConnection.setAutoCommit( false );
}
}
catch (SQLException e) {
log.info( "Was unable to reset JDBC connection to no longer be in auto-commit mode" );
}
}
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
}
}

View File

@ -63,8 +63,14 @@ public class ScriptTargetOutputToFile extends ScriptTargetOutputToWriter impleme
@SuppressWarnings("ResultOfMethodCallIgnored")
static Writer toFileWriter(File file) {
try {
// best effort, since this is very well not allowed in EE environments
file.createNewFile();
if ( ! file.exists() ) {
// best effort, since this is very likely not allowed in EE environments
log.debug( "Attempting to create non-existent script target file : " + file.getAbsolutePath() );
if ( file.getParentFile() != null ) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
}
catch (Exception e) {
log.debug( "Exception calling File#createNewFile : " + e.toString() );

View File

@ -27,12 +27,20 @@ import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.Writer;
import org.hibernate.internal.util.StringHelper;
/**
* ScriptTargetOutput implementation for supplied Writer references
*
* @author Steve Ebersole
*/
public class ScriptTargetOutputToWriter implements ScriptTargetOutput {
private static final String NEWLINE;
static {
final String systemNewLine = System.getProperty( "line.separator" );
NEWLINE = StringHelper.isNotEmpty( systemNewLine ) ? systemNewLine : "\n";
}
private final Writer writer;
/**
@ -48,6 +56,7 @@ public class ScriptTargetOutputToWriter implements ScriptTargetOutput {
public void accept(String command) {
try {
writer.write( command );
writer.write( NEWLINE );
writer.flush();
}
catch (IOException e) {