HHH-7169 - Use ClassLoaderService to load JDBC driver classes

This commit is contained in:
Steve Ebersole 2012-03-13 14:51:04 -05:00
parent c3fc7f3214
commit 87941b89aa
2 changed files with 58 additions and 53 deletions

View File

@ -23,24 +23,28 @@
*/ */
package org.hibernate.service.jdbc.connections.internal; package org.hibernate.service.jdbc.connections.internal;
import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.HibernateException; import com.mchange.v2.c3p0.DataSources;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.Stoppable;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import com.mchange.v2.c3p0.DataSources;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable;
/** /**
* A connection provider that uses a C3P0 connection pool. Hibernate will use this by * A connection provider that uses a C3P0 connection pool. Hibernate will use this by
@ -49,7 +53,8 @@ import com.mchange.v2.c3p0.DataSources;
* @author various people * @author various people
* @see ConnectionProvider * @see ConnectionProvider
*/ */
public class C3P0ConnectionProvider implements ConnectionProvider, Configurable, Stoppable { public class C3P0ConnectionProvider
implements ConnectionProvider, Configurable, Stoppable, ServiceRegistryAwareService {
private static final C3P0MessageLogger LOG = Logger.getMessageLogger(C3P0MessageLogger.class, C3P0ConnectionProvider.class.getName()); private static final C3P0MessageLogger LOG = Logger.getMessageLogger(C3P0MessageLogger.class, C3P0ConnectionProvider.class.getName());
@ -72,9 +77,10 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
private Integer isolation; private Integer isolation;
private boolean autocommit; private boolean autocommit;
/** private ServiceRegistryImplementor serviceRegistry;
* {@inheritDoc}
*/ @Override
@SuppressWarnings("UnnecessaryUnboxing")
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
final Connection c = ds.getConnection(); final Connection c = ds.getConnection();
if ( isolation != null ) { if ( isolation != null ) {
@ -86,9 +92,7 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
return c; return c;
} }
/** @Override
* {@inheritDoc}
*/
public void closeConnection(Connection conn) throws SQLException { public void closeConnection(Connection conn) throws SQLException {
conn.close(); conn.close();
} }
@ -115,9 +119,6 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
} }
} }
/**
* {@inheritDoc}
*/
@Override @Override
@SuppressWarnings( {"unchecked"}) @SuppressWarnings( {"unchecked"})
public void configure(Map props) { public void configure(Map props) {
@ -131,20 +132,15 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
autocommit = ConfigurationHelper.getBoolean( Environment.AUTOCOMMIT, props ); autocommit = ConfigurationHelper.getBoolean( Environment.AUTOCOMMIT, props );
LOG.autoCommitMode( autocommit ); LOG.autoCommitMode( autocommit );
if (jdbcDriverClass == null) LOG.jdbcDriverNotSpecified(Environment.DRIVER); if (jdbcDriverClass == null) {
LOG.jdbcDriverNotSpecified(Environment.DRIVER);
}
else { else {
try { try {
Class.forName( jdbcDriverClass ); serviceRegistry.getService( ClassLoaderService.class ).classForName( jdbcDriverClass );
} }
catch ( ClassNotFoundException cnfe ) { catch ( ClassLoadingException e ) {
try { throw new ClassLoadingException( LOG.jdbcDriverNotFound(jdbcDriverClass), e );
ReflectHelper.classForName( jdbcDriverClass );
}
catch ( ClassNotFoundException e ) {
String msg = LOG.jdbcDriverNotFound(jdbcDriverClass);
LOG.error(msg, e);
throw new HibernateException( msg, e );
}
} }
} }
@ -162,8 +158,11 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
// turn hibernate.c3p0.* into c3p0.*, so c3p0 // turn hibernate.c3p0.* into c3p0.*, so c3p0
// gets a chance to see all hibernate.c3p0.* // gets a chance to see all hibernate.c3p0.*
for ( Iterator ii = props.keySet().iterator(); ii.hasNext(); ) { for ( Object o : props.keySet() ) {
String key = ( String ) ii.next(); if ( ! String.class.isInstance( o ) ) {
continue;
}
final String key = (String) o;
if ( key.startsWith( "hibernate.c3p0." ) ) { if ( key.startsWith( "hibernate.c3p0." ) ) {
String newKey = key.substring( 15 ); String newKey = key.substring( 15 );
if ( props.containsKey( newKey ) ) { if ( props.containsKey( newKey ) ) {
@ -218,9 +217,6 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
} }
/**
*
*/
public void close() { public void close() {
try { try {
DataSources.destroy( ds ); DataSources.destroy( ds );
@ -230,9 +226,7 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
} }
} }
/** @Override
* {@inheritDoc}
*/
public boolean supportsAggressiveRelease() { public boolean supportsAggressiveRelease() {
return false; return false;
} }
@ -259,4 +253,9 @@ public class C3P0ConnectionProvider implements ConnectionProvider, Configurable,
public void stop() { public void stop() {
close(); close();
} }
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
} }

View File

@ -36,11 +36,14 @@ import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
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.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable; import org.hibernate.service.spi.Stoppable;
/** /**
@ -53,8 +56,8 @@ import org.hibernate.service.spi.Stoppable;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@SuppressWarnings( {"UnnecessaryUnboxing"}) @SuppressWarnings( {"UnnecessaryUnboxing"})
public class DriverManagerConnectionProviderImpl implements ConnectionProvider, Configurable, Stoppable { public class DriverManagerConnectionProviderImpl
implements ConnectionProvider, Configurable, Stoppable, ServiceRegistryAwareService {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, DriverManagerConnectionProviderImpl.class.getName() ); private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, DriverManagerConnectionProviderImpl.class.getName() );
private String url; private String url;
@ -68,6 +71,8 @@ public class DriverManagerConnectionProviderImpl implements ConnectionProvider,
private boolean stopped; private boolean stopped;
private transient ServiceRegistryImplementor serviceRegistry;
@Override @Override
public boolean isUnwrappableAs(Class unwrapType) { public boolean isUnwrappableAs(Class unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) || return ConnectionProvider.class.equals( unwrapType ) ||
@ -90,19 +95,15 @@ public class DriverManagerConnectionProviderImpl implements ConnectionProvider,
LOG.usingHibernateBuiltInConnectionPool(); LOG.usingHibernateBuiltInConnectionPool();
String driverClassName = (String) configurationValues.get( AvailableSettings.DRIVER ); String driverClassName = (String) configurationValues.get( AvailableSettings.DRIVER );
if (driverClassName == null) LOG.jdbcDriverNotSpecified(AvailableSettings.DRIVER); if (driverClassName == null) {
LOG.jdbcDriverNotSpecified( AvailableSettings.DRIVER );
}
else { else {
try { try {
// trying via forName() first to be as close to DriverManager's semantics serviceRegistry.getService( ClassLoaderService.class ).classForName( driverClassName );
Class.forName( driverClassName );
} }
catch ( ClassNotFoundException cnfe ) { catch ( ClassLoadingException e ) {
try { throw new ClassLoadingException( "Specified JDBC Driver " + driverClassName + " class not found", e );
ReflectHelper.classForName( driverClassName );
}
catch ( ClassNotFoundException e ) {
throw new HibernateException( "Specified JDBC Driver " + driverClassName + " class not found", e );
}
} }
} }
@ -214,4 +215,9 @@ public class DriverManagerConnectionProviderImpl implements ConnectionProvider,
public boolean supportsAggressiveRelease() { public boolean supportsAggressiveRelease() {
return false; return false;
} }
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
} }