HHH-8159 - Apply fixups indicated by analysis tools

This commit is contained in:
Steve Ebersole 2013-04-30 08:29:06 -05:00
parent d2ec514894
commit 9b37f2a297
4 changed files with 130 additions and 81 deletions

View File

@ -31,6 +31,7 @@ import java.util.Properties;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources; import com.mchange.v2.c3p0.DataSources;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -56,22 +57,25 @@ import org.hibernate.service.spi.Stoppable;
public class C3P0ConnectionProvider public class C3P0ConnectionProvider
implements ConnectionProvider, Configurable, Stoppable, ServiceRegistryAwareService { 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()
);
//swaldman 2006-08-28: define c3p0-style configuration parameters for properties with //swaldman 2006-08-28: define c3p0-style configuration parameters for properties with
// hibernate-specific overrides to detect and warn about conflicting // hibernate-specific overrides to detect and warn about conflicting
// declarations // declarations
private final static String C3P0_STYLE_MIN_POOL_SIZE = "c3p0.minPoolSize"; private static final String C3P0_STYLE_MIN_POOL_SIZE = "c3p0.minPoolSize";
private final static String C3P0_STYLE_MAX_POOL_SIZE = "c3p0.maxPoolSize"; private static final String C3P0_STYLE_MAX_POOL_SIZE = "c3p0.maxPoolSize";
private final static String C3P0_STYLE_MAX_IDLE_TIME = "c3p0.maxIdleTime"; private static final String C3P0_STYLE_MAX_IDLE_TIME = "c3p0.maxIdleTime";
private final static String C3P0_STYLE_MAX_STATEMENTS = "c3p0.maxStatements"; private static final String C3P0_STYLE_MAX_STATEMENTS = "c3p0.maxStatements";
private final static String C3P0_STYLE_ACQUIRE_INCREMENT = "c3p0.acquireIncrement"; private static final String C3P0_STYLE_ACQUIRE_INCREMENT = "c3p0.acquireIncrement";
private final static String C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD = "c3p0.idleConnectionTestPeriod"; private static final String C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD = "c3p0.idleConnectionTestPeriod";
//swaldman 2006-08-28: define c3p0-style configuration parameters for initialPoolSize, which //swaldman 2006-08-28: define c3p0-style configuration parameters for initialPoolSize, which
// hibernate sensibly lets default to minPoolSize, but we'll let users // hibernate sensibly lets default to minPoolSize, but we'll let users
// override it with the c3p0-style property if they want. // override it with the c3p0-style property if they want.
private final static String C3P0_STYLE_INITIAL_POOL_SIZE = "c3p0.initialPoolSize"; private static final String C3P0_STYLE_INITIAL_POOL_SIZE = "c3p0.initialPoolSize";
private DataSource ds; private DataSource ds;
private Integer isolation; private Integer isolation;
@ -105,7 +109,7 @@ public class C3P0ConnectionProvider
} }
@Override @Override
@SuppressWarnings( {"unchecked"}) @SuppressWarnings({"unchecked"})
public <T> T unwrap(Class<T> unwrapType) { public <T> T unwrap(Class<T> unwrapType) {
if ( ConnectionProvider.class.equals( unwrapType ) || if ( ConnectionProvider.class.equals( unwrapType ) ||
C3P0ConnectionProvider.class.isAssignableFrom( unwrapType ) ) { C3P0ConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
@ -120,46 +124,46 @@ public class C3P0ConnectionProvider
} }
@Override @Override
@SuppressWarnings( {"unchecked"}) @SuppressWarnings({"unchecked"})
public void configure(Map props) { public void configure(Map props) {
String jdbcDriverClass = (String) props.get( Environment.DRIVER ); final String jdbcDriverClass = (String) props.get( Environment.DRIVER );
String jdbcUrl = (String) props.get( Environment.URL ); final String jdbcUrl = (String) props.get( Environment.URL );
Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( props ); final Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( props );
LOG.c3p0UsingDriver(jdbcDriverClass, jdbcUrl); LOG.c3p0UsingDriver( jdbcDriverClass, jdbcUrl );
LOG.connectionProperties(ConfigurationHelper.maskOut(connectionProps, "password")); LOG.connectionProperties( ConfigurationHelper.maskOut( connectionProps, "password" ) );
autocommit = ConfigurationHelper.getBoolean( Environment.AUTOCOMMIT, props ); autocommit = ConfigurationHelper.getBoolean( Environment.AUTOCOMMIT, props );
LOG.autoCommitMode( autocommit ); LOG.autoCommitMode( autocommit );
if (jdbcDriverClass == null) { if ( jdbcDriverClass == null ) {
LOG.jdbcDriverNotSpecified(Environment.DRIVER); LOG.jdbcDriverNotSpecified( Environment.DRIVER );
} }
else { else {
try { try {
serviceRegistry.getService( ClassLoaderService.class ).classForName( jdbcDriverClass ); serviceRegistry.getService( ClassLoaderService.class ).classForName( jdbcDriverClass );
} }
catch ( ClassLoadingException e ) { catch (ClassLoadingException e) {
throw new ClassLoadingException( LOG.jdbcDriverNotFound(jdbcDriverClass), e ); throw new ClassLoadingException( LOG.jdbcDriverNotFound( jdbcDriverClass ), e );
} }
} }
try { try {
//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults //swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
Integer minPoolSize = ConfigurationHelper.getInteger( Environment.C3P0_MIN_SIZE, props ); final Integer minPoolSize = ConfigurationHelper.getInteger( Environment.C3P0_MIN_SIZE, props );
Integer maxPoolSize = ConfigurationHelper.getInteger( Environment.C3P0_MAX_SIZE, props ); final Integer maxPoolSize = ConfigurationHelper.getInteger( Environment.C3P0_MAX_SIZE, props );
Integer maxIdleTime = ConfigurationHelper.getInteger( Environment.C3P0_TIMEOUT, props ); final Integer maxIdleTime = ConfigurationHelper.getInteger( Environment.C3P0_TIMEOUT, props );
Integer maxStatements = ConfigurationHelper.getInteger( Environment.C3P0_MAX_STATEMENTS, props ); final Integer maxStatements = ConfigurationHelper.getInteger( Environment.C3P0_MAX_STATEMENTS, props );
Integer acquireIncrement = ConfigurationHelper.getInteger( Environment.C3P0_ACQUIRE_INCREMENT, props ); final Integer acquireIncrement = ConfigurationHelper.getInteger( Environment.C3P0_ACQUIRE_INCREMENT, props );
Integer idleTestPeriod = ConfigurationHelper.getInteger( Environment.C3P0_IDLE_TEST_PERIOD, props ); final Integer idleTestPeriod = ConfigurationHelper.getInteger( Environment.C3P0_IDLE_TEST_PERIOD, props );
Properties c3props = new Properties(); final Properties c3props = new Properties();
// 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 ( Object o : props.keySet() ) { for ( Object o : props.keySet() ) {
if ( ! String.class.isInstance( o ) ) { if ( !String.class.isInstance( o ) ) {
continue; continue;
} }
final String key = (String) o; final String key = (String) o;
@ -182,63 +186,62 @@ public class C3P0ConnectionProvider
Environment.C3P0_ACQUIRE_INCREMENT, C3P0_STYLE_ACQUIRE_INCREMENT, props, c3props, acquireIncrement Environment.C3P0_ACQUIRE_INCREMENT, C3P0_STYLE_ACQUIRE_INCREMENT, props, c3props, acquireIncrement
); );
setOverwriteProperty( setOverwriteProperty(
Environment.C3P0_IDLE_TEST_PERIOD, C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD, props, c3props, idleTestPeriod Environment.C3P0_IDLE_TEST_PERIOD,
C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD,
props,
c3props,
idleTestPeriod
); );
// revert to traditional hibernate behavior of setting initialPoolSize to minPoolSize // revert to traditional hibernate behavior of setting initialPoolSize to minPoolSize
// unless otherwise specified with a c3p0.*-style parameter. // unless otherwise specified with a c3p0.*-style parameter.
Integer initialPoolSize = ConfigurationHelper.getInteger( C3P0_STYLE_INITIAL_POOL_SIZE, props ); final Integer initialPoolSize = ConfigurationHelper.getInteger( C3P0_STYLE_INITIAL_POOL_SIZE, props );
if ( initialPoolSize == null && minPoolSize != null ) { if ( initialPoolSize == null && minPoolSize != null ) {
c3props.put( C3P0_STYLE_INITIAL_POOL_SIZE, String.valueOf( minPoolSize ).trim() ); c3props.put( C3P0_STYLE_INITIAL_POOL_SIZE, String.valueOf( minPoolSize ).trim() );
} }
/*DataSource unpooled = DataSources.unpooledDataSource( final DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl, connectionProps );
jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)
);*/
DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl, connectionProps );
Map allProps = new HashMap(); final Map allProps = new HashMap();
allProps.putAll( props ); allProps.putAll( props );
allProps.putAll( c3props ); allProps.putAll( c3props );
ds = DataSources.pooledDataSource( unpooled, allProps ); ds = DataSources.pooledDataSource( unpooled, allProps );
} }
catch ( Exception e ) { catch (Exception e) {
LOG.error(LOG.unableToInstantiateC3p0ConnectionPool(), e); LOG.error( LOG.unableToInstantiateC3p0ConnectionPool(), e );
throw new HibernateException(LOG.unableToInstantiateC3p0ConnectionPool(), e); throw new HibernateException( LOG.unableToInstantiateC3p0ConnectionPool(), e );
} }
String i = (String) props.get( Environment.ISOLATION ); final String i = (String) props.get( Environment.ISOLATION );
if (i == null) isolation = null; if ( i == null ) {
isolation = null;
}
else { else {
isolation = Integer.valueOf( i ); isolation = Integer.valueOf( i );
LOG.jdbcIsolationLevel(Environment.isolationLevelToString(isolation)); LOG.jdbcIsolationLevel( Environment.isolationLevelToString( isolation ) );
} }
} }
public void close() {
try {
DataSources.destroy( ds );
}
catch ( SQLException sqle ) {
LOG.unableToDestroyC3p0ConnectionPool(sqle);
}
}
@Override @Override
public boolean supportsAggressiveRelease() { public boolean supportsAggressiveRelease() {
return false; return false;
} }
private void setOverwriteProperty(String hibernateStyleKey, String c3p0StyleKey, Map hibp, Properties c3p, Integer value) { private void setOverwriteProperty(
String hibernateStyleKey,
String c3p0StyleKey,
Map hibp,
Properties c3p,
Integer value) {
if ( value != null ) { if ( value != null ) {
String peeledC3p0Key = c3p0StyleKey.substring(5); final String peeledC3p0Key = c3p0StyleKey.substring( 5 );
c3p.put( peeledC3p0Key, String.valueOf( value ).trim() ); c3p.put( peeledC3p0Key, String.valueOf( value ).trim() );
if ( hibp.containsKey( c3p0StyleKey ) ) { if ( hibp.containsKey( c3p0StyleKey ) ) {
warnPropertyConflict( hibernateStyleKey, c3p0StyleKey ); warnPropertyConflict( hibernateStyleKey, c3p0StyleKey );
} }
String longC3p0StyleKey = "hibernate." + c3p0StyleKey; final String longC3p0StyleKey = "hibernate." + c3p0StyleKey;
if ( hibp.containsKey( longC3p0StyleKey ) ) { if ( hibp.containsKey( longC3p0StyleKey ) ) {
warnPropertyConflict( hibernateStyleKey, longC3p0StyleKey ); warnPropertyConflict( hibernateStyleKey, longC3p0StyleKey );
} }
@ -246,12 +249,27 @@ public class C3P0ConnectionProvider
} }
private void warnPropertyConflict(String hibernateStyle, String c3p0Style) { private void warnPropertyConflict(String hibernateStyle, String c3p0Style) {
LOG.bothHibernateAndC3p0StylesSet(hibernateStyle, c3p0Style, hibernateStyle, c3p0Style); LOG.bothHibernateAndC3p0StylesSet( hibernateStyle, c3p0Style, hibernateStyle, c3p0Style );
} }
@Override @Override
public void stop() { public void stop() {
close(); try {
DataSources.destroy( ds );
}
catch (SQLException sqle) {
LOG.unableToDestroyC3p0ConnectionPool( sqle );
}
}
/**
* Close the provider.
*
* @deprecated Use {@link #stop} instead
*/
@Deprecated
public void close() {
stop();
} }
@Override @Override

View File

@ -41,29 +41,54 @@ import static org.jboss.logging.Logger.Level.WARN;
* <p/> * <p/>
* New messages must be added after the last message defined to ensure message codes are unique. * New messages must be added after the last message defined to ensure message codes are unique.
*/ */
@MessageLogger( projectCode = "HHH" ) @MessageLogger(projectCode = "HHH")
public interface C3P0MessageLogger extends CoreMessageLogger { public interface C3P0MessageLogger extends CoreMessageLogger {
@LogMessage( level = WARN ) /**
@Message( value = "Both hibernate-style property '%s' and c3p0-style property '%s' have been set in hibernate.properties. " * Log a message (WARN) about conflicting {@code hibernate.c3p0.XYZ} and {@code c3p0.XYZ} settings
+ "Hibernate-style property '%s' will be used and c3p0-style property '%s' will be ignored!", id = 10001 ) *
void bothHibernateAndC3p0StylesSet( String hibernateStyle, * @param hibernateStyle The {@code hibernate.c3p0} prefixed setting
String c3p0Style, * @param c3p0Style The {@code c3p0.} prefixed setting
String hibernateStyle2, */
String c3p0Style2 ); @LogMessage(level = WARN)
@Message(value = "Both hibernate-style property '%1$s' and c3p0-style property '%2$s' have been set in Hibernate "
+ "properties. Hibernate-style property '%1$s' will be used and c3p0-style property '2$%s' will be ignored!", id = 10001)
void bothHibernateAndC3p0StylesSet(String hibernateStyle,String c3p0Style);
@LogMessage( level = INFO ) /**
@Message( value = "C3P0 using driver: %s at URL: %s", id = 10002 ) * Log a message (INFO) about which Driver class is being used.
void c3p0UsingDriver( String jdbcDriverClass, *
String jdbcUrl ); * @param jdbcDriverClass The JDBC Driver class
* @param jdbcUrl The JDBC URL
*/
@LogMessage(level = INFO)
@Message(value = "C3P0 using driver: %s at URL: %s", id = 10002)
void c3p0UsingDriver(String jdbcDriverClass, String jdbcUrl);
@Message( value = "JDBC Driver class not found: %s", id = 10003 ) /**
String jdbcDriverNotFound( String jdbcDriverClass ); * Build a message about not being able to find the JDBC driver class
*
* @param jdbcDriverClass The JDBC driver class we could not find
*
* @return The message
*/
@Message(value = "JDBC Driver class not found: %s", id = 10003)
String jdbcDriverNotFound(String jdbcDriverClass);
@LogMessage( level = WARN ) /**
@Message( value = "Could not destroy C3P0 connection pool", id = 10004 ) * Log a message (WARN) about not being able to stop the underlying c3p0 pool.
void unableToDestroyC3p0ConnectionPool( @Cause SQLException e ); *
* @param e The exception when we tried to stop pool
*/
@LogMessage(level = WARN)
@Message(value = "Could not destroy C3P0 connection pool", id = 10004)
void unableToDestroyC3p0ConnectionPool(@Cause SQLException e);
@Message( value = "Could not instantiate C3P0 connection pool", id = 10005 ) /**
String unableToInstantiateC3p0ConnectionPool(); * Build a message about not being able to start the underlying c3p0 pool.
*
* @return The message
*/
@Message(value = "Could not instantiate C3P0 connection pool", id = 10005)
String unableToInstantiateC3p0ConnectionPool();
} }

View File

@ -38,16 +38,18 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
* @author Brett Meyer * @author Brett Meyer
*/ */
public class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider { public class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
private static final List<StrategyRegistration> REGISTRATIONS = Collections.singletonList( private static final List<StrategyRegistration> REGISTRATIONS = Collections.singletonList(
(StrategyRegistration) new SimpleStrategyRegistrationImpl( (StrategyRegistration) new SimpleStrategyRegistrationImpl<ConnectionProvider>(
ConnectionProvider.class, ConnectionProvider.class,
C3P0ConnectionProvider.class, C3P0ConnectionProvider.class,
"c3p0", "c3p0",
C3P0ConnectionProvider.class.getSimpleName(), C3P0ConnectionProvider.class.getSimpleName(),
"org.hibernate.connection.C3P0ConnectionProvider", // legacy // legacy
"org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" // legacy "org.hibernate.connection.C3P0ConnectionProvider",
) ); // legacy
"org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider"
)
);
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -0,0 +1,4 @@
/**
* Implementation of ConnectionProvider using the c3p0 Connection pool.
*/
package org.hibernate.c3p0.internal;