From: <ste...@us...> - 2006-02-10 03:48:47
|
Update of /cvsroot/hibernate/Hibernate3/src/org/hibernate/tool/hbm2ddl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12646/src/org/hibernate/tool/hbm2ddl Modified Files: SchemaExport.java SchemaUpdate.java SchemaValidator.java Added Files: ConnectionHelper.java ManagedProviderConnectionHelper.java SuppliedConnectionHelper.java SuppliedConnectionProviderConnectionHelper.java Log Message: HHH-1445 : unified connection handling amongst core tools --- NEW FILE: ConnectionHelper.java --- package org.hibernate.tool.hbm2ddl; import java.sql.Connection; import java.sql.SQLException; /** * Contract for delegates responsible for managing connection used by the * hbm2ddl tools. * * @author Steve Ebersole */ interface ConnectionHelper { /** * Prepare the helper for use. * * @param needsAutoCommit Should connection be forced to auto-commit * if not already. * @throws SQLException */ public void prepare(boolean needsAutoCommit) throws SQLException; /** * Get a reference to the connection we are using. * * @return The JDBC connection. * @throws SQLException */ public Connection getConnection() throws SQLException; /** * Release any resources held by this helper. * * @throws SQLException */ public void release() throws SQLException; } --- NEW FILE: ManagedProviderConnectionHelper.java --- package org.hibernate.tool.hbm2ddl; import org.hibernate.connection.ConnectionProvider; import org.hibernate.connection.ConnectionProviderFactory; import org.hibernate.util.JDBCExceptionReporter; import java.util.Properties; import java.sql.Connection; import java.sql.SQLException; /** * A {@link ConnectionHelper} implementation based on an internally * built and managed {@link ConnectionProvider}. * * @author Steve Ebersole */ class ManagedProviderConnectionHelper implements ConnectionHelper { private Properties cfgProperties; private ConnectionProvider connectionProvider; private Connection connection; public ManagedProviderConnectionHelper(Properties cfgProperties) { this.cfgProperties = cfgProperties; } public void prepare(boolean needsAutoCommit) throws SQLException { connectionProvider = ConnectionProviderFactory.newConnectionProvider( cfgProperties ); connection = connectionProvider.getConnection(); if ( needsAutoCommit && !connection.getAutoCommit() ) { connection.commit(); connection.setAutoCommit( true ); } } public Connection getConnection() throws SQLException { return connection; } public void release() throws SQLException { if ( connection != null ) { try { JDBCExceptionReporter.logAndClearWarnings( connection ); connectionProvider.closeConnection( connection ); } finally { connectionProvider.close(); } } connection = null; } } --- NEW FILE: SuppliedConnectionHelper.java --- package org.hibernate.tool.hbm2ddl; import org.hibernate.util.JDBCExceptionReporter; import java.sql.Connection; import java.sql.SQLException; /** * A {@link ConnectionHelper} implementation based on an explicitly supplied * connection. * * @author Steve Ebersole */ class SuppliedConnectionHelper implements ConnectionHelper { private Connection connection; private boolean toggleAutoCommit; public SuppliedConnectionHelper(Connection connection) { this.connection = connection; } public void prepare(boolean needsAutoCommit) throws SQLException { toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit(); if ( toggleAutoCommit ) { try { connection.commit(); } catch( Throwable ignore ) { // might happen with a managed connection } connection.setAutoCommit( true ); } } public Connection getConnection() { return connection; } public void release() throws SQLException { JDBCExceptionReporter.logAndClearWarnings( connection ); if ( toggleAutoCommit ) { connection.setAutoCommit( false ); } connection = null; } } --- NEW FILE: SuppliedConnectionProviderConnectionHelper.java --- package org.hibernate.tool.hbm2ddl; import org.hibernate.connection.ConnectionProvider; import org.hibernate.util.JDBCExceptionReporter; import java.sql.Connection; import java.sql.SQLException; /** * A {@link ConnectionHelper} implementation based on a provided * {@link ConnectionProvider}. Essentially, ensures that the connection * gets cleaned up, but that the provider itself remains usable since it * was externally provided to us. * * @author Steve Ebersole */ class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper { private ConnectionProvider provider; private Connection connection; private boolean toggleAutoCommit; public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) { this.provider = provider; } public void prepare(boolean needsAutoCommit) throws SQLException { connection = provider.getConnection(); toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit(); if ( toggleAutoCommit ) { try { connection.commit(); } catch( Throwable ignore ) { // might happen with a managed connection } connection.setAutoCommit( true ); } } public Connection getConnection() throws SQLException { return connection; } public void release() throws SQLException { // we only release the connection if ( connection != null ) { JDBCExceptionReporter.logAndClearWarnings( connection ); if ( toggleAutoCommit ) { connection.setAutoCommit( false ); } provider.closeConnection( connection ); connection = null; } } } Index: SchemaExport.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/tool/hbm2ddl/SchemaExport.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -d -r1.23 -r1.24 --- SchemaExport.java 30 Oct 2005 10:59:20 -0000 1.23 +++ SchemaExport.java 10 Feb 2006 03:48:38 -0000 1.24 @@ -14,10 +14,8 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Properties; -import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -27,12 +25,9 @@ import org.hibernate.cfg.Environment; import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.Settings; -import org.hibernate.connection.ConnectionProvider; -import org.hibernate.connection.ConnectionProviderFactory; import org.hibernate.dialect.Dialect; import org.hibernate.pretty.DDLFormatter; import org.hibernate.util.ConfigHelper; -import org.hibernate.util.JDBCExceptionReporter; import org.hibernate.util.PropertiesHelper; import org.hibernate.util.ReflectHelper; @@ -44,7 +39,7 @@ */ public class SchemaExport { - private static final Log log = LogFactory.getLog(SchemaExport.class); + private static final Log log = LogFactory.getLog( SchemaExport.class ); private ConnectionHelper connectionHelper; private String[] dropSQL; @@ -53,9 +48,9 @@ private String importFile = "/import.sql"; private Dialect dialect; private String delimiter; - private final List exceptions = new ArrayList(); - private boolean haltOnError = false; - private boolean format = true; + private final List exceptions = new ArrayList(); + private boolean haltOnError = false; + private boolean format = true; /** * Create a schema exporter for the given Configuration @@ -70,9 +65,9 @@ */ public SchemaExport(Configuration cfg, Settings settings) throws HibernateException { dialect = settings.getDialect(); - connectionHelper = new SuppliedConnectionProviderConnectionHelper( - settings.getConnectionProvider() - ); + connectionHelper = new SuppliedConnectionProviderConnectionHelper( + settings.getConnectionProvider() + ); dropSQL = cfg.generateDropSchemaScript( dialect ); createSQL = cfg.generateSchemaCreationScript( dialect ); format = settings.isFormatSqlEnabled(); @@ -81,21 +76,21 @@ /** * Create a schema exporter for the given Configuration, with the given * database connection properties. - * + * * @deprecated properties may be specified via the Configuration object */ - public SchemaExport(Configuration cfg, Properties properties) - throws HibernateException { + public SchemaExport(Configuration cfg, Properties properties) + throws HibernateException { dialect = Dialect.getDialect( properties ); Properties props = new Properties(); props.putAll( dialect.getDefaultProperties() ); props.putAll( properties ); - connectionHelper = new ProviderConnectionHelper( props ); + connectionHelper = new ManagedProviderConnectionHelper( props ); dropSQL = cfg.generateDropSchemaScript( dialect ); createSQL = cfg.generateSchemaCreationScript( dialect ); - format = PropertiesHelper.getBoolean(Environment.FORMAT_SQL, props); + format = PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ); } public SchemaExport(Configuration cfg, Connection connection) { @@ -128,174 +123,188 @@ /** * Run the schema creation script. + * * @param script print the DDL to the console * @param export export the script to the database */ public void create(boolean script, boolean export) { - execute(script, export, false, false); + execute( script, export, false, false ); } /** * Run the drop schema script. + * * @param script print the DDL to the console * @param export export the script to the database */ public void drop(boolean script, boolean export) { - execute(script, export, true, false); + execute( script, export, true, false ); } - + private String format(String sql) { - return format ? - new DDLFormatter(sql).format() : - sql; + return format ? + new DDLFormatter( sql ).format() : + sql; } public void execute(boolean script, boolean export, boolean justDrop, boolean justCreate) { - log.info("Running hbm2ddl schema export"); + log.info( "Running hbm2ddl schema export" ); Connection connection = null; Writer outputFileWriter = null; Reader importFileReader = null; Statement statement = null; - exceptions.clear(); + exceptions.clear(); try { - - try - { - InputStream stream = ConfigHelper.getResourceAsStream(importFile); - importFileReader = new InputStreamReader(stream); + + try { + InputStream stream = ConfigHelper.getResourceAsStream( importFile ); + importFileReader = new InputStreamReader( stream ); } - catch (HibernateException e) - { - log.debug("import file not found: " + importFile); + catch ( HibernateException e ) { + log.debug( "import file not found: " + importFile ); } - if (outputFile != null) { - log.info("writing generated schema to file: " + outputFile); - outputFileWriter = new FileWriter(outputFile); + if ( outputFile != null ) { + log.info( "writing generated schema to file: " + outputFile ); + outputFileWriter = new FileWriter( outputFile ); } - if (export) { - log.info("exporting generated schema to database"); + if ( export ) { + log.info( "exporting generated schema to database" ); + connectionHelper.prepare( true ); connection = connectionHelper.getConnection(); - if ( !connection.getAutoCommit() ) { - connection.commit(); - connection.setAutoCommit(true); - } statement = connection.createStatement(); } - - if (!justCreate) { + + if ( !justCreate ) { drop( script, export, outputFileWriter, statement ); } - if (!justDrop) { + if ( !justDrop ) { create( script, export, outputFileWriter, statement ); - if (export && importFileReader!=null) { - importScript(importFileReader, statement); + if ( export && importFileReader != null ) { + importScript( importFileReader, statement ); } } - log.info("schema export complete"); + log.info( "schema export complete" ); } - catch(Exception e) { - exceptions.add(e); - log.error("schema export unsuccessful", e); + catch ( Exception e ) { + exceptions.add( e ); + log.error( "schema export unsuccessful", e ); } finally { try { - if (statement!=null) statement.close(); - if (connection!=null) connectionHelper.release(); + if ( statement != null ) { + statement.close(); + } + if ( connection != null ) { + connectionHelper.release(); + } } - catch(Exception e) { - exceptions.add(e); + catch ( Exception e ) { + exceptions.add( e ); log.error( "Could not close connection", e ); } try { - if (outputFileWriter!=null) outputFileWriter.close(); - if (importFileReader!=null) importFileReader.close(); + if ( outputFileWriter != null ) { + outputFileWriter.close(); + } + if ( importFileReader != null ) { + importFileReader.close(); + } } - catch (IOException ioe) { - exceptions.add(ioe); + catch ( IOException ioe ) { + exceptions.add( ioe ); log.error( "Error closing output file: " + outputFile, ioe ); } } } - - private void importScript(Reader importFileReader, Statement statement) + + private void importScript(Reader importFileReader, Statement statement) throws IOException { - log.info("Executing import script: " + importFile); - BufferedReader reader = new BufferedReader(importFileReader); - for ( String sql = reader.readLine(); sql!=null; sql = reader.readLine() ) { + log.info( "Executing import script: " + importFile ); + BufferedReader reader = new BufferedReader( importFileReader ); + for ( String sql = reader.readLine(); sql != null; sql = reader.readLine() ) { try { String trimmedSql = sql.trim(); - if(trimmedSql.length()==0 || - trimmedSql.startsWith("--") || - trimmedSql.startsWith("//") || - trimmedSql.startsWith("/*")) { + if ( trimmedSql.length() == 0 || + trimmedSql.startsWith( "--" ) || + trimmedSql.startsWith( "//" ) || + trimmedSql.startsWith( "/*" ) ) { continue; - } else { - if(trimmedSql.endsWith(";")) { - trimmedSql = trimmedSql.substring(0,trimmedSql.length()-1); + } + else { + if ( trimmedSql.endsWith( ";" ) ) { + trimmedSql = trimmedSql.substring( 0, trimmedSql.length() - 1 ); } - log.debug(trimmedSql); - statement.execute(trimmedSql); + log.debug( trimmedSql ); + statement.execute( trimmedSql ); } } - catch (SQLException e) { - throw new JDBCException("Error during import script execution", e); - } + catch ( SQLException e ) { + throw new JDBCException( "Error during import script execution", e ); + } } } - private void create(boolean script, boolean export, Writer fileOutput, Statement statement) + private void create(boolean script, boolean export, Writer fileOutput, Statement statement) throws IOException { - for (int j = 0; j < createSQL.length; j++) { + for ( int j = 0; j < createSQL.length; j++ ) { try { execute( script, export, fileOutput, statement, createSQL[j] ); } - catch (SQLException e) { - if (haltOnError) { - throw new JDBCException("Error during DDL export", e); + catch ( SQLException e ) { + if ( haltOnError ) { + throw new JDBCException( "Error during DDL export", e ); } - exceptions.add(e); + exceptions.add( e ); log.error( "Unsuccessful: " + createSQL[j] ); log.error( e.getMessage() ); } } } - private void drop(boolean script, boolean export, Writer fileOutput, Statement statement) + private void drop(boolean script, boolean export, Writer fileOutput, Statement statement) throws IOException { - for (int i = 0; i < dropSQL.length; i++) { + for ( int i = 0; i < dropSQL.length; i++ ) { try { execute( script, export, fileOutput, statement, dropSQL[i] ); } - catch (SQLException e) { - exceptions.add(e); + catch ( SQLException e ) { + exceptions.add( e ); log.debug( "Unsuccessful: " + dropSQL[i] ); log.debug( e.getMessage() ); } } } - private void execute(boolean script, boolean export, Writer fileOutput, Statement statement, final String sql) + private void execute(boolean script, boolean export, Writer fileOutput, Statement statement, final String sql) throws IOException, SQLException { String formatted = format( sql ); - if (delimiter != null) formatted += delimiter; - if (script) System.out.println(formatted); - log.debug(formatted); - if (outputFile != null) fileOutput.write( formatted + "\n" ); - if (export) statement.executeUpdate( sql ); + if ( delimiter != null ) { + formatted += delimiter; + } + if ( script ) { + System.out.println( formatted ); + } + log.debug( formatted ); + if ( outputFile != null ) { + fileOutput.write( formatted + "\n" ); + } + if ( export ) { + statement.executeUpdate( sql ); + } } public static void main(String[] args) { @@ -313,185 +322,100 @@ boolean format = false; String delim = null; - for ( int i=0; i<args.length; i++ ) { - if( args[i].startsWith("--") ) { - if( args[i].equals("--quiet") ) { + for ( int i = 0; i < args.length; i++ ) { + if ( args[i].startsWith( "--" ) ) { + if ( args[i].equals( "--quiet" ) ) { script = false; } - else if( args[i].equals("--drop") ) { + else if ( args[i].equals( "--drop" ) ) { drop = true; } - else if( args[i].equals("--create") ) { + else if ( args[i].equals( "--create" ) ) { create = true; } - else if( args[i].equals("--haltonerror") ) { + else if ( args[i].equals( "--haltonerror" ) ) { halt = true; } - else if( args[i].equals("--text") ) { + else if ( args[i].equals( "--text" ) ) { export = false; } - else if( args[i].startsWith("--output=") ) { - outFile = args[i].substring(9); + else if ( args[i].startsWith( "--output=" ) ) { + outFile = args[i].substring( 9 ); } - else if( args[i].startsWith("--import=") ) { - importFile = args[i].substring(9); + else if ( args[i].startsWith( "--import=" ) ) { + importFile = args[i].substring( 9 ); } - else if( args[i].startsWith("--properties=") ) { - propFile = args[i].substring(13); + else if ( args[i].startsWith( "--properties=" ) ) { + propFile = args[i].substring( 13 ); } - else if( args[i].equals("--format") ) { + else if ( args[i].equals( "--format" ) ) { format = true; } - else if ( args[i].startsWith("--delimiter=") ) { - delim = args[i].substring(12); + else if ( args[i].startsWith( "--delimiter=" ) ) { + delim = args[i].substring( 12 ); } - else if ( args[i].startsWith("--config=") ) { - cfg.configure( args[i].substring(9) ); + else if ( args[i].startsWith( "--config=" ) ) { + cfg.configure( args[i].substring( 9 ) ); } - else if ( args[i].startsWith("--naming=") ) { + else if ( args[i].startsWith( "--naming=" ) ) { cfg.setNamingStrategy( - (NamingStrategy) ReflectHelper.classForName( args[i].substring(9) ) + ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ) .newInstance() - ); + ); } } else { String filename = args[i]; if ( filename.endsWith( ".jar" ) ) { - cfg.addJar( new File(filename) ); + cfg.addJar( new File( filename ) ); } else { - cfg.addFile(filename); + cfg.addFile( filename ); } } } - - if (propFile!=null) { + + if ( propFile != null ) { Properties props = new Properties(); props.putAll( cfg.getProperties() ); - props.load( new FileInputStream(propFile) ); - cfg.setProperties(props); + props.load( new FileInputStream( propFile ) ); + cfg.setProperties( props ); } - SchemaExport se = new SchemaExport(cfg) - .setHaltOnError(halt) - .setOutputFile(outFile) - .setImportFile(importFile) - .setDelimiter(delim); - if (format) se.setFormat(true); - se.execute(script, export, drop, create); - + SchemaExport se = new SchemaExport( cfg ) + .setHaltOnError( halt ) + .setOutputFile( outFile ) + .setImportFile( importFile ) + .setDelimiter( delim ); + if ( format ) { + se.setFormat( true ); + } + se.execute( script, export, drop, create ); + } - catch(Exception e) { + catch ( Exception e ) { log.error( "Error creating schema ", e ); e.printStackTrace(); } } - /** - * Returns a List of all Exceptions which occured during the export. - * @return A List containig the Exceptions occured during the export - */ - public List getExceptions() { - return exceptions; - } - - private interface ConnectionHelper { - Connection getConnection() throws SQLException; - void release() throws SQLException; - } - - private class SuppliedConnectionHelper implements ConnectionHelper { - private Connection connection; - - public SuppliedConnectionHelper(Connection connection) { - this.connection = connection; - } - - public Connection getConnection() { - return connection; - } - - public void release() { - JDBCExceptionReporter.logAndClearWarnings(connection); - connection = null; - } + /** + * Returns a List of all Exceptions which occured during the export. + * + * @return A List containig the Exceptions occured during the export + */ + public List getExceptions() { + return exceptions; } - private class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper { - private ConnectionProvider provider; - private Connection connection; - - public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) { - this.provider = provider; - } - - public Connection getConnection() throws SQLException { - if (connection == null) { - connection = provider.getConnection(); - if ( !connection.getAutoCommit() ) { - connection.commit(); - connection.setAutoCommit( true ); - } - } - return connection; - } - - public void release() throws SQLException { - if (connection != null) { - JDBCExceptionReporter.logAndClearWarnings(connection); - provider.closeConnection( connection ); - connection = null; - } - } - } - public SchemaExport setFormat(boolean format) { this.format = format; return this; } - private class ProviderConnectionHelper implements ConnectionHelper { - private Properties cfgProperties; - private ConnectionProvider connectionProvider; - private Connection connection; - - public ProviderConnectionHelper(Properties cfgProperties) { - this.cfgProperties = cfgProperties; - } - - public Connection getConnection() throws SQLException { - if ( connection == null ) { - connectionProvider = ConnectionProviderFactory.newConnectionProvider( cfgProperties ); - connection = connectionProvider.getConnection(); - if ( !connection.getAutoCommit() ) { - connection.commit(); - connection.setAutoCommit( true ); - } - } - return connection; - } - - public void release() throws SQLException { - if ( connection!=null ) { - JDBCExceptionReporter.logAndClearWarnings(connection); - connectionProvider.closeConnection( connection ); - connectionProvider.close(); - } - connection = null; - } - } - public SchemaExport setHaltOnError(boolean haltOnError) { this.haltOnError = haltOnError; return this; } } - - - - - - - Index: SchemaUpdate.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/tool/hbm2ddl/SchemaUpdate.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- SchemaUpdate.java 11 Aug 2005 20:41:21 -0000 1.5 +++ SchemaUpdate.java 10 Feb 2006 03:48:38 -0000 1.6 @@ -15,8 +15,6 @@ import org.hibernate.cfg.Configuration; import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.Settings; -import org.hibernate.connection.ConnectionProvider; -import org.hibernate.connection.ConnectionProviderFactory; import org.hibernate.dialect.Dialect; import org.hibernate.util.ReflectHelper; @@ -28,11 +26,11 @@ */ public class SchemaUpdate { - private static final Log log = LogFactory.getLog(SchemaUpdate.class); - private ConnectionProvider connectionProvider; + private static final Log log = LogFactory.getLog( SchemaUpdate.class ); + private ConnectionHelper connectionHelper; private Configuration configuration; private Dialect dialect; - private List exceptions; + private List exceptions; public SchemaUpdate(Configuration cfg) throws HibernateException { this( cfg, cfg.getProperties() ); @@ -40,19 +38,21 @@ public SchemaUpdate(Configuration cfg, Properties connectionProperties) throws HibernateException { this.configuration = cfg; - dialect = Dialect.getDialect(connectionProperties); + dialect = Dialect.getDialect( connectionProperties ); Properties props = new Properties(); props.putAll( dialect.getDefaultProperties() ); - props.putAll(connectionProperties); - connectionProvider = ConnectionProviderFactory.newConnectionProvider(props); - exceptions = new ArrayList(); + props.putAll( connectionProperties ); + connectionHelper = new ManagedProviderConnectionHelper( props ); + exceptions = new ArrayList(); } public SchemaUpdate(Configuration cfg, Settings settings) throws HibernateException { this.configuration = cfg; dialect = settings.getDialect(); - connectionProvider = settings.getConnectionProvider(); - exceptions = new ArrayList(); + connectionHelper = new SuppliedConnectionProviderConnectionHelper( + settings.getConnectionProvider() + ); + exceptions = new ArrayList(); } public static void main(String[] args) { @@ -64,42 +64,42 @@ boolean doUpdate = true; String propFile = null; - for ( int i=0; i<args.length; i++ ) { - if( args[i].startsWith("--") ) { - if( args[i].equals("--quiet") ) { + for ( int i = 0; i < args.length; i++ ) { + if ( args[i].startsWith( "--" ) ) { + if ( args[i].equals( "--quiet" ) ) { script = false; } - else if( args[i].startsWith("--properties=") ) { - propFile = args[i].substring(13); + else if ( args[i].startsWith( "--properties=" ) ) { + propFile = args[i].substring( 13 ); } - else if ( args[i].startsWith("--config=") ) { - cfg.configure( args[i].substring(9) ); + else if ( args[i].startsWith( "--config=" ) ) { + cfg.configure( args[i].substring( 9 ) ); } - else if ( args[i].startsWith("--text") ) { + else if ( args[i].startsWith( "--text" ) ) { doUpdate = false; } - else if ( args[i].startsWith("--naming=") ) { + else if ( args[i].startsWith( "--naming=" ) ) { cfg.setNamingStrategy( - (NamingStrategy) ReflectHelper.classForName( args[i].substring(9) ).newInstance() + ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance() ); } } else { - cfg.addFile(args[i]); + cfg.addFile( args[i] ); } } - - if (propFile!=null) { + + if ( propFile != null ) { Properties props = new Properties(); props.putAll( cfg.getProperties() ); - props.load( new FileInputStream(propFile) ); - cfg.setProperties(props); + props.load( new FileInputStream( propFile ) ); + cfg.setProperties( props ); } - new SchemaUpdate(cfg).execute(script, doUpdate); + new SchemaUpdate( cfg ).execute( script, doUpdate ); } - catch (Exception e) { + catch ( Exception e ) { log.error( "Error running schema update", e ); e.printStackTrace(); } @@ -107,92 +107,85 @@ /** * Execute the schema updates + * * @param script print all DDL to the console */ public void execute(boolean script, boolean doUpdate) { - log.info("Running hbm2ddl schema update"); + log.info( "Running hbm2ddl schema update" ); - Connection connection=null; - Statement stmt=null; - boolean autoCommitWasEnabled = true; + Connection connection = null; + Statement stmt = null; - exceptions.clear(); + exceptions.clear(); try { DatabaseMetadata meta; try { - log.info("fetching database metadata"); - connection = connectionProvider.getConnection(); - if ( !connection.getAutoCommit() ) { - connection.commit(); - connection.setAutoCommit(true); - autoCommitWasEnabled = false; - } - meta = new DatabaseMetadata(connection, dialect); + log.info( "fetching database metadata" ); + connectionHelper.prepare( true ); + connection = connectionHelper.getConnection(); + meta = new DatabaseMetadata( connection, dialect ); stmt = connection.createStatement(); } - catch (SQLException sqle) { - exceptions.add(sqle); - log.error("could not get database metadata", sqle); + catch ( SQLException sqle ) { + exceptions.add( sqle ); + log.error( "could not get database metadata", sqle ); throw sqle; } - log.info("updating schema"); + log.info( "updating schema" ); - String[] createSQL = configuration.generateSchemaUpdateScript(dialect, meta); - for (int j = 0; j < createSQL.length; j++) { + String[] createSQL = configuration.generateSchemaUpdateScript( dialect, meta ); + for ( int j = 0; j < createSQL.length; j++ ) { final String sql = createSQL[j]; try { - if (script) System.out.println(sql); - if (doUpdate) { - log.debug(sql); - stmt.executeUpdate(sql); + if ( script ) { + System.out.println( sql ); + } + if ( doUpdate ) { + log.debug( sql ); + stmt.executeUpdate( sql ); } } - catch (SQLException e) { - exceptions.add(e); + catch ( SQLException e ) { + exceptions.add( e ); log.error( "Unsuccessful: " + sql ); log.error( e.getMessage() ); } } - log.info("schema update complete"); + log.info( "schema update complete" ); } - catch (Exception e) { - exceptions.add(e); - log.error("could not complete schema update", e); + catch ( Exception e ) { + exceptions.add( e ); + log.error( "could not complete schema update", e ); } finally { try { - if (stmt!=null) stmt.close(); - if (!autoCommitWasEnabled) connection.setAutoCommit(false); - if (connection!=null) connection.close(); - if (connectionProvider!=null) connectionProvider.close(); + if ( stmt != null ) { + stmt.close(); + } + connectionHelper.release(); } - catch (Exception e) { - exceptions.add(e); - log.error("Error closing connection", e); + catch ( Exception e ) { + exceptions.add( e ); + log.error( "Error closing connection", e ); } } } - /** - * Returns a List of all Exceptions which occured during the export. - * @return A List containig the Exceptions occured during the export - */ - public List getExceptions() { - return exceptions; - } + /** + * Returns a List of all Exceptions which occured during the export. + * + * @return A List containig the Exceptions occured during the export + */ + public List getExceptions() { + return exceptions; + } } - - - - - - Index: SchemaValidator.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/tool/hbm2ddl/SchemaValidator.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- SchemaValidator.java 7 Oct 2005 08:35:44 -0000 1.3 +++ SchemaValidator.java 10 Feb 2006 03:48:38 -0000 1.4 @@ -12,8 +12,6 @@ import org.hibernate.cfg.Configuration; import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.Settings; -import org.hibernate.connection.ConnectionProvider; -import org.hibernate.connection.ConnectionProviderFactory; import org.hibernate.dialect.Dialect; import org.hibernate.util.ReflectHelper; @@ -25,8 +23,8 @@ */ public class SchemaValidator { - private static final Log log = LogFactory.getLog(SchemaValidator.class); - private ConnectionProvider connectionProvider; + private static final Log log = LogFactory.getLog( SchemaValidator.class ); + private ConnectionHelper connectionHelper; private Configuration configuration; private Dialect dialect; @@ -36,17 +34,19 @@ public SchemaValidator(Configuration cfg, Properties connectionProperties) throws HibernateException { this.configuration = cfg; - dialect = Dialect.getDialect(connectionProperties); + dialect = Dialect.getDialect( connectionProperties ); Properties props = new Properties(); props.putAll( dialect.getDefaultProperties() ); - props.putAll(connectionProperties); - connectionProvider = ConnectionProviderFactory.newConnectionProvider(props); + props.putAll( connectionProperties ); + connectionHelper = new ManagedProviderConnectionHelper( props ); } public SchemaValidator(Configuration cfg, Settings settings) throws HibernateException { this.configuration = cfg; dialect = settings.getDialect(); - connectionProvider = settings.getConnectionProvider(); + connectionHelper = new SuppliedConnectionProviderConnectionHelper( + settings.getConnectionProvider() + ); } public static void main(String[] args) { @@ -55,87 +55,80 @@ String propFile = null; - for ( int i=0; i<args.length; i++ ) { - if( args[i].startsWith("--") ) { - if( args[i].startsWith("--properties=") ) { - propFile = args[i].substring(13); + for ( int i = 0; i < args.length; i++ ) { + if ( args[i].startsWith( "--" ) ) { + if ( args[i].startsWith( "--properties=" ) ) { + propFile = args[i].substring( 13 ); } - else if ( args[i].startsWith("--config=") ) { - cfg.configure( args[i].substring(9) ); + else if ( args[i].startsWith( "--config=" ) ) { + cfg.configure( args[i].substring( 9 ) ); } - else if ( args[i].startsWith("--naming=") ) { + else if ( args[i].startsWith( "--naming=" ) ) { cfg.setNamingStrategy( - (NamingStrategy) ReflectHelper.classForName( args[i].substring(9) ).newInstance() + ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance() ); } } else { - cfg.addFile(args[i]); + cfg.addFile( args[i] ); } } - - if (propFile!=null) { + + if ( propFile != null ) { Properties props = new Properties(); props.putAll( cfg.getProperties() ); - props.load( new FileInputStream(propFile) ); - cfg.setProperties(props); + props.load( new FileInputStream( propFile ) ); + cfg.setProperties( props ); } - - new SchemaValidator(cfg).validate(); + + new SchemaValidator( cfg ).validate(); } - catch (Exception e) { + catch ( Exception e ) { log.error( "Error running schema update", e ); e.printStackTrace(); } } /** - * Execute the schema updates - * @param script print all DDL to the console + * Perform the validations. */ public void validate() { - log.info("Running schema validator"); + log.info( "Running schema validator" ); - Connection connection=null; + Connection connection = null; try { DatabaseMetadata meta; try { - log.info("fetching database metadata"); - connection = connectionProvider.getConnection(); - meta = new DatabaseMetadata(connection, dialect, false); + log.info( "fetching database metadata" ); + connectionHelper.prepare( false ); + connection = connectionHelper.getConnection(); + meta = new DatabaseMetadata( connection, dialect, false ); } - catch (SQLException sqle) { - log.error("could not get database metadata", sqle); + catch ( SQLException sqle ) { + log.error( "could not get database metadata", sqle ); throw sqle; } - - configuration.validateSchema(dialect, meta); + + configuration.validateSchema( dialect, meta ); } - catch (SQLException e) { - log.error("could not complete schema validation", e); + catch ( SQLException e ) { + log.error( "could not complete schema validation", e ); } finally { try { - if (connection!=null) connection.close(); - if (connectionProvider!=null) connectionProvider.close(); + connectionHelper.release(); } - catch (Exception e) { - log.error("Error closing connection", e); + catch ( Exception e ) { + log.error( "Error closing connection", e ); } } } } - - - - - - |