Merge branch '5.0' of github.com:hibernate/hibernate-orm into 5.0

This commit is contained in:
Steve Ebersole 2016-01-13 13:01:28 -06:00
commit 028aae9593
7 changed files with 140 additions and 4 deletions

View File

@ -130,7 +130,12 @@ public class SchemaUpdate {
);
}
schemaMigrator.doMigration( metadata, databaseInformation, true, toolTargets );
try {
schemaMigrator.doMigration( metadata, databaseInformation, true, toolTargets );
}
finally {
databaseInformation.cleanup();
}
}
private List<org.hibernate.tool.schema.spi.Target> buildToolTargets(Target target) {

View File

@ -86,9 +86,13 @@ public class SchemaValidator {
"Error creating DatabaseInformation for schema validation"
);
}
serviceRegistry.getService( SchemaManagementTool.class ).getSchemaValidator( cfgService.getSettings() )
.doValidation( metadata, databaseInformation );
try {
serviceRegistry.getService( SchemaManagementTool.class ).getSchemaValidator( cfgService.getSettings() )
.doValidation( metadata, databaseInformation );
}
finally {
databaseInformation.cleanup();
}
}
public static void main(String[] args) {

View File

@ -152,4 +152,9 @@ public class DatabaseInformationImpl implements DatabaseInformation, ExtractionC
return sequenceInformationMap.get( sequenceName );
}
@Override
public void cleanup() {
extractionContext.cleanup();
}
}

View File

@ -96,6 +96,7 @@ public class ExtractionContextImpl implements ExtractionContext {
return registeredTableAccess;
}
@Override
public void cleanup() {
if ( jdbcDatabaseMetaData != null ) {
jdbcDatabaseMetaData = null;

View File

@ -101,4 +101,6 @@ public interface DatabaseInformation {
* @return {@code true} indicates a catalog with the given name already exists
*/
boolean catalogExists(Identifier catalog);
void cleanup();
}

View File

@ -40,4 +40,6 @@ public interface ExtractionContext {
}
DatabaseObjectAccess getDatabaseObjectAccess();
void cleanup();
}

View File

@ -0,0 +1,117 @@
/*
* 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.test.schemaupdate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.SchemaValidator;
import org.hibernate.tool.hbm2ddl.Target;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-10443")
public class ConnectionsReleaseTest extends BaseUnitTestCase {
public static Properties getConnectionProviderProperties() {
Properties props = new Properties();
props.put( Environment.DRIVER, "org.h2.Driver" );
props.put( Environment.URL, String.format( "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", "db1" ) );
props.put( Environment.USER, "sa" );
props.put( Environment.PASS, "" );
return props;
}
private StandardServiceRegistry ssr;
private MetadataImplementor metadata;
private ConnectionProviderDecorator connectionProvider;
@Before
public void setUp() {
connectionProvider = new ConnectionProviderDecorator();
connectionProvider.configure( getConnectionProviderProperties() );
ssr = new StandardServiceRegistryBuilder()
.addService( ConnectionProvider.class, connectionProvider )
.build();
metadata = (MetadataImplementor) new MetadataSources( ssr )
.addAnnotatedClass( Thing.class )
.buildMetadata();
metadata.validate();
}
@After
public void tearDown() {
StandardServiceRegistryBuilder.destroy( ssr );
}
@Test
public void testSchemaUpdateReleasesAllConnections() throws SQLException {
new SchemaUpdate( ssr, metadata ).execute( Target.EXPORT );
assertThat( connectionProvider.getOpenConnection(), is( 0 ) );
}
@Test
public void testSchemaValidatorReleasesAllConnections() throws SQLException {
new SchemaValidator( ssr, metadata ).validate();
assertThat( connectionProvider.getOpenConnection(), is( 0 ) );
}
@Entity(name = "Thing")
@Table(name = "Thing")
public static class Thing {
@Id
public Integer id;
}
public static class ConnectionProviderDecorator extends DriverManagerConnectionProviderImpl {
private int openConnection;
@Override
public Connection getConnection() throws SQLException {
openConnection++;
return super.getConnection();
}
@Override
public void closeConnection(Connection conn) throws SQLException {
super.closeConnection( conn );
openConnection--;
}
public int getOpenConnection() {
return this.openConnection;
}
}
}