HHH-8203 HHH-8204 Hibernate 4.x can't work with proxool 0.9.1

This commit is contained in:
Strong Liu 2013-04-25 19:02:51 +08:00
parent dfc8c4b10f
commit 932d85ba26
4 changed files with 95 additions and 23 deletions

View File

@ -26,6 +26,7 @@ package org.hibernate.service.jdbc.connections.internal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -41,13 +42,15 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.UnknownUnwrapTypeException; import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.Stoppable;
/** /**
* A connection provider that uses a Proxool connection pool. Hibernate will use this by * A connection provider that uses a Proxool connection pool. Hibernate will use this by
* default if the <tt>hibernate.proxool.*</tt> properties are set. * default if the <tt>hibernate.proxool.*</tt> properties are set.
* @see ConnectionProvider * @see ConnectionProvider
*/ */
public class ProxoolConnectionProvider implements ConnectionProvider { public class ProxoolConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
public static final ProxoolMessageLogger LOG = Logger.getMessageLogger(ProxoolMessageLogger.class, ProxoolConnectionProvider.class.getName()); public static final ProxoolMessageLogger LOG = Logger.getMessageLogger(ProxoolMessageLogger.class, ProxoolConnectionProvider.class.getName());
@ -69,12 +72,13 @@ public class ProxoolConnectionProvider implements ConnectionProvider {
* @return a JDBC connection * @return a JDBC connection
* @throws SQLException * @throws SQLException
*/ */
@Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
// get a connection from the pool (thru DriverManager, cfr. Proxool doc) // get a connection from the pool (thru DriverManager, cfr. Proxool doc)
Connection c = DriverManager.getConnection(proxoolAlias); Connection c = DriverManager.getConnection(proxoolAlias);
// set the Transaction Isolation if defined // set the Transaction Isolation if defined
if (isolation!=null) c.setTransactionIsolation( isolation.intValue() ); if (isolation!=null) c.setTransactionIsolation( isolation );
// toggle autoCommit to false if set // toggle autoCommit to false if set
if ( c.getAutoCommit()!=autocommit ) c.setAutoCommit(autocommit); if ( c.getAutoCommit()!=autocommit ) c.setAutoCommit(autocommit);
@ -106,6 +110,7 @@ public class ProxoolConnectionProvider implements ConnectionProvider {
* @param conn a JDBC connection * @param conn a JDBC connection
* @throws SQLException * @throws SQLException
*/ */
@Override
public void closeConnection(Connection conn) throws SQLException { public void closeConnection(Connection conn) throws SQLException {
conn.close(); conn.close();
} }
@ -114,15 +119,16 @@ public class ProxoolConnectionProvider implements ConnectionProvider {
* Initialize the connection provider from given properties. * Initialize the connection provider from given properties.
* @param props <tt>SessionFactory</tt> properties * @param props <tt>SessionFactory</tt> properties
*/ */
public void configure(Properties props) throws HibernateException { @Override
public void configure(Map props) {
// Get the configurator files (if available) // Get the configurator files (if available)
String jaxpFile = props.getProperty(Environment.PROXOOL_XML); String jaxpFile = (String)props.get(Environment.PROXOOL_XML);
String propFile = props.getProperty(Environment.PROXOOL_PROPERTIES); String propFile = (String)props.get(Environment.PROXOOL_PROPERTIES);
String externalConfig = props.getProperty(Environment.PROXOOL_EXISTING_POOL); String externalConfig = (String)props.get(Environment.PROXOOL_EXISTING_POOL);
// Default the Proxool alias setting // Default the Proxool alias setting
proxoolAlias = props.getProperty(Environment.PROXOOL_POOL_ALIAS); proxoolAlias = (String)props.get(Environment.PROXOOL_POOL_ALIAS);
// Configured outside of Hibernate (i.e. Servlet container, or Java Bean Container // Configured outside of Hibernate (i.e. Servlet container, or Java Bean Container
// already has Proxool pools running, and this provider is to just borrow one of these // already has Proxool pools running, and this provider is to just borrow one of these
@ -235,8 +241,14 @@ public class ProxoolConnectionProvider implements ConnectionProvider {
/** /**
* @see ConnectionProvider#supportsAggressiveRelease() * @see ConnectionProvider#supportsAggressiveRelease()
*/ */
@Override
public boolean supportsAggressiveRelease() { public boolean supportsAggressiveRelease() {
return false; return false;
} }
@Override
public void stop() {
close();
}
} }

View File

@ -25,12 +25,14 @@ package org.hibernate.service.jdbc.connections.internal;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties;
import org.junit.Test; import org.junit.Test;
import org.logicalcobwebs.proxool.ProxoolFacade; import org.logicalcobwebs.proxool.ProxoolFacade;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -42,22 +44,25 @@ import static org.junit.Assert.assertTrue;
* @author Sanne Grinovero * @author Sanne Grinovero
*/ */
public class ProxoolConnectionProviderTest extends BaseUnitTestCase { public class ProxoolConnectionProviderTest extends BaseUnitTestCase {
@Test @Test
public void testPoolsClosed() { public void testPoolsClosed() {
assertDefinedPools(); // zero-length-vararg used as parameter assertDefinedPools(); // zero-length-vararg used as parameter
StandardServiceRegistry serviceRegistry = buildServiceRegistry( "pool-one" );
ProxoolConnectionProvider providerOne = new ProxoolConnectionProvider(); ConnectionProvider providerOne = serviceRegistry.getService( ConnectionProvider.class );
providerOne.configure( getPoolConfigurarion( "pool-one" ) );
assertDefinedPools( "pool-one" ); assertDefinedPools( "pool-one" );
ProxoolConnectionProvider providerTwo = new ProxoolConnectionProvider();
providerTwo.configure( getPoolConfigurarion( "pool-two" ) ); StandardServiceRegistry serviceRegistryTwo = buildServiceRegistry( "pool-two" );
ConnectionProvider providerTwo = serviceRegistryTwo.getService( ConnectionProvider.class );
assertDefinedPools( "pool-one", "pool-two" ); assertDefinedPools( "pool-one", "pool-two" );
providerOne.close(); StandardServiceRegistryBuilder.destroy( serviceRegistry );
assertDefinedPools( "pool-two" ); assertDefinedPools( "pool-two" );
providerTwo.close(); StandardServiceRegistryBuilder.destroy( serviceRegistryTwo );
assertDefinedPools(); assertDefinedPools();
} }
@ -69,11 +74,14 @@ public class ProxoolConnectionProviderTest extends BaseUnitTestCase {
} }
} }
private Properties getPoolConfigurarion(String poolName) {
Properties cfg = new Properties(); private StandardServiceRegistry buildServiceRegistry(String poolName){
cfg.setProperty( Environment.PROXOOL_POOL_ALIAS, poolName );
cfg.setProperty( Environment.PROXOOL_PROPERTIES, poolName + ".properties" ); return new StandardServiceRegistryBuilder( )
return cfg; .applySetting( Environment.PROXOOL_POOL_ALIAS, poolName )
} .applySetting( Environment.PROXOOL_PROPERTIES, poolName + ".properties" )
.applySetting( Environment.CONNECTION_PROVIDER, ProxoolConnectionProvider.class.getName() )
.build();
} }
}

View File

@ -0,0 +1,42 @@
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# Copyright (c) 2010, 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
#
hibernate.dialect org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class org.h2.Driver
hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
hibernate.connection.username sa
hibernate.connection.pool_size 5
hibernate.jdbc.batch_size 10
hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider
hibernate.proxool.properties pool-one.properties
hibernate.show_sql false
hibernate.max_fetch_depth 5
hibernate.cache.region_prefix hibernate.test
hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory
# NOTE: hibernate.jdbc.batch_versioned_data should be set to false when testing with Oracle
hibernate.jdbc.batch_versioned_data true

View File

@ -0,0 +1,10 @@
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=info, stdout
log4j.logger.org.hibernate.tool.hbm2ddl=debug
log4j.logger.org.hibernate.testing.cache=debug