From 3e3250e2a96c9f098626ba4b72e3d0fe143d48ab Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 23 Sep 2011 11:04:21 -0700 Subject: [PATCH] HHH-6669 : Manual SchemaExport broken due to temp code comments --- .../ManagedProviderConnectionHelper.java | 52 +++++++++++++---- .../SchemaExportManagedConnectionTest.java | 37 ++++++++++++ .../SchemaExportSuppliedConnectionTest.java | 57 +++++++++++++++++++ .../test/schemaupdate/SchemaExportTest.java | 29 ++-------- 4 files changed, 139 insertions(+), 36 deletions(-) create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportManagedConnectionTest.java create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportSuppliedConnectionTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java index 59ecac80b6..b40b1a7f8b 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java @@ -27,9 +27,13 @@ package org.hibernate.tool.hbm2ddl; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; + +import org.hibernate.cfg.Environment; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; +import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.service.ServiceRegistryBuilder; +import org.hibernate.service.internal.BasicServiceRegistryImpl; import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; -import org.hibernate.service.spi.Stoppable; /** * A {@link ConnectionHelper} implementation based on an internally @@ -39,7 +43,7 @@ import org.hibernate.service.spi.Stoppable; */ class ManagedProviderConnectionHelper implements ConnectionHelper { private Properties cfgProperties; - private ConnectionProvider connectionProvider; + private BasicServiceRegistryImpl serviceRegistry; private Connection connection; public ManagedProviderConnectionHelper(Properties cfgProperties) { @@ -47,14 +51,18 @@ class ManagedProviderConnectionHelper implements ConnectionHelper { } public void prepare(boolean needsAutoCommit) throws SQLException { - /* TEMP TEMP TEMP - connectionProvider = ConnectionProviderBuilder.buildConnectionProvider(); - connection = connectionProvider.getConnection(); - if ( needsAutoCommit && !connection.getAutoCommit() ) { + serviceRegistry = createServiceRegistry( cfgProperties ); + connection = serviceRegistry.getService( ConnectionProvider.class ).getConnection(); + if ( needsAutoCommit && ! connection.getAutoCommit() ) { connection.commit(); connection.setAutoCommit( true ); } - */ + } + + private static BasicServiceRegistryImpl createServiceRegistry(Properties properties) { + Environment.verifyProperties( properties ); + ConfigurationHelper.resolvePlaceHolders( properties ); + return (BasicServiceRegistryImpl) new ServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry(); } public Connection getConnection() throws SQLException { @@ -62,18 +70,38 @@ class ManagedProviderConnectionHelper implements ConnectionHelper { } public void release() throws SQLException { + try { + releaseConnection(); + } + finally { + releaseServiceRegistry(); + } + } + + private void releaseConnection() throws SQLException { if ( connection != null ) { try { new SqlExceptionHelper().logAndClearWarnings( connection ); - connectionProvider.closeConnection( connection ); } finally { - if ( connectionProvider instanceof Stoppable ) { - ( ( Stoppable ) connectionProvider ).stop(); + try { + serviceRegistry.getService( ConnectionProvider.class ).closeConnection( connection ); + } + finally { + connection = null; } - connectionProvider = null; } } - connection = null; + } + + private void releaseServiceRegistry() { + if ( serviceRegistry != null ) { + try { + serviceRegistry.destroy(); + } + finally { + serviceRegistry = null; + } + } } } diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportManagedConnectionTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportManagedConnectionTest.java new file mode 100644 index 0000000000..da607b6e15 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportManagedConnectionTest.java @@ -0,0 +1,37 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.schemaupdate; + +import org.hibernate.cfg.Configuration; +import org.hibernate.tool.hbm2ddl.SchemaExport; + +/** + * @author Gail Badner + */ +public class SchemaExportManagedConnectionTest extends SchemaExportTest { + @Override + protected SchemaExport createSchemaExport(Configuration cfg) { + return new SchemaExport( cfg ); + } +} \ No newline at end of file diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportSuppliedConnectionTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportSuppliedConnectionTest.java new file mode 100644 index 0000000000..92e9c291e6 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportSuppliedConnectionTest.java @@ -0,0 +1,57 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.schemaupdate; + +import org.junit.After; +import org.junit.Before; + +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.tool.hbm2ddl.SchemaExport; + +/** + * @author Gail Badner + */ +public class SchemaExportSuppliedConnectionTest extends SchemaExportTest { + + private ServiceRegistry serviceRegistry; + + @Before + public void setUp() { + serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() ); + } + + @After + public void tearDown() { + ServiceRegistryBuilder.destroy( serviceRegistry ); + serviceRegistry = null; + } + + @Override + protected SchemaExport createSchemaExport(Configuration cfg) { + return new SchemaExport( serviceRegistry, cfg ); + } +} \ No newline at end of file diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportTest.java index 298e5a2e03..a010aebce5 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/schemaupdate/SchemaExportTest.java @@ -28,10 +28,6 @@ import org.junit.Before; import org.junit.Test; import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.engine.jdbc.spi.JdbcServices; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.tool.hbm2ddl.SchemaExport; @@ -40,31 +36,16 @@ import static org.junit.Assert.assertEquals; /** * @author Gail Badner */ -public class SchemaExportTest extends BaseUnitTestCase { +public abstract class SchemaExportTest extends BaseUnitTestCase { private final String MAPPING = "org/hibernate/test/schemaupdate/mapping.hbm.xml"; - private ServiceRegistry serviceRegistry; - - @Before - public void setUp() { - serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() ); - } - - @After - public void tearDown() { - ServiceRegistryBuilder.destroy( serviceRegistry ); - serviceRegistry = null; - } - - protected JdbcServices getJdbcServices() { - return serviceRegistry.getService( JdbcServices.class ); - } + protected abstract SchemaExport createSchemaExport(Configuration cfg); @Test public void testCreateAndDropOnlyType() { Configuration cfg = new Configuration(); cfg.addResource( MAPPING ); - SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg ); + SchemaExport schemaExport = createSchemaExport( cfg ); // create w/o dropping first; (OK because tables don't exist yet schemaExport.execute( false, true, false, true ); assertEquals( 0, schemaExport.getExceptions().size() ); @@ -82,7 +63,7 @@ public class SchemaExportTest extends BaseUnitTestCase { public void testBothType() { Configuration cfg = new Configuration(); cfg.addResource( MAPPING ); - SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg ); + SchemaExport schemaExport = createSchemaExport( cfg ); // drop before create (nothing to drop yeT) schemaExport.execute( false, true, false, false ); assertEquals( 0, schemaExport.getExceptions().size() ); @@ -98,7 +79,7 @@ public class SchemaExportTest extends BaseUnitTestCase { public void testCreateAndDrop() { Configuration cfg = new Configuration(); cfg.addResource( MAPPING ); - SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg ); + SchemaExport schemaExport = createSchemaExport( cfg ); // should drop before creating, but tables don't exist yet schemaExport.create( false, true); assertEquals( 0, schemaExport.getExceptions().size() );