Author: tchemit Date: 2013-07-13 23:08:38 +0200 (Sat, 13 Jul 2013) New Revision: 2762 Url: http://nuiton.org/projects/topia/repository/revisions/2762 Log: fixes #2758: Improved TopiaConnectionProvider Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2013-07-13 21:07:58 UTC (rev 2761) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2013-07-13 21:08:38 UTC (rev 2762) @@ -24,6 +24,18 @@ */ package org.nuiton.topia.framework; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.HibernateException; +import org.hibernate.cfg.Environment; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.service.UnknownUnwrapTypeException; +import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; +import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.service.spi.Configurable; +import org.hibernate.service.spi.Stoppable; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -32,15 +44,6 @@ import java.util.Map; import java.util.Properties; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.HibernateException; -import org.hibernate.cfg.Environment; -import org.hibernate.internal.util.ReflectHelper; -import org.hibernate.internal.util.config.ConfigurationHelper; -import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; -import org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl; - /** * Customized connection provider. * <p/> @@ -62,22 +65,24 @@ * @author tchemit <chemit@codelutin.com> * @since 2.5.3 */ -public class TopiaConnectionProvider extends DriverManagerConnectionProviderImpl { +public class TopiaConnectionProvider implements ConnectionProvider, Configurable, Stoppable {//}, ServiceRegistryAwareService { /** Logger. */ private static final Log log = LogFactory.getLog(TopiaConnectionProvider.class); + private static final long serialVersionUID = -8190835231054317644L; + /** * JDBC url of connection. * <p/> * This is a mandatory hibernate configuration vi the property * {@link Environment#URL}. */ - private String url; + protected String url; /** All grabbed connection properties */ - private Properties connectionProps; + protected Properties connectionProps; /** * Sql isolation level to use in connection. @@ -86,7 +91,7 @@ * * @see Connection#getTransactionIsolation() */ - private Integer isolation; + protected Integer isolation; /** * auto commit connection state. @@ -95,7 +100,7 @@ * * @see Connection#getAutoCommit() */ - private boolean autocommit; + protected boolean autocommit; /** * Size of connection pool. @@ -103,18 +108,22 @@ * By default use {@code 20}, can be specify by using the hibernate * configuration property {@link Environment#POOL_SIZE}. */ - private int poolSize; + protected int poolSize; + private boolean stopped; + /** Our pool of connections which are not closed and availables. */ - private final List<Connection> pool; + protected final List<Connection> pool; +// protected ServiceRegistryImplementor serviceRegistry; + public TopiaConnectionProvider() { pool = new ArrayList<Connection>(); } @Override public void configure(Map configurationValues) throws HibernateException { - String driverClass = (String)configurationValues.get(Environment.DRIVER); + String driverClass = (String) configurationValues.get(Environment.DRIVER); poolSize = ConfigurationHelper.getInt(Environment.POOL_SIZE, configurationValues, 20); //default pool size 20 if (log.isDebugEnabled()) { @@ -129,7 +138,7 @@ if (isolation != null) { if (log.isDebugEnabled()) { log.debug("JDBC isolation level: " + - Environment.isolationLevelToString(isolation)); + Environment.isolationLevelToString(isolation)); } } @@ -154,7 +163,7 @@ } } - url = (String)configurationValues.get(Environment.URL); + url = (String) configurationValues.get(Environment.URL); if (url == null) { String msg = "JDBC URL was not specified by property " + Environment.URL; @@ -175,7 +184,7 @@ log.debug("connection properties: " + connectionProps); } else if (log.isDebugEnabled()) { log.debug("connection properties: " + - ConfigurationHelper.maskOut(connectionProps, "password")); + ConfigurationHelper.maskOut(connectionProps, "password")); } } @@ -272,8 +281,10 @@ @Override protected void finalize() throws Throwable { + if (!stopped) { + stop(); + } super.finalize(); - stop(); } @Override @@ -293,7 +304,7 @@ } } pool.clear(); - + stopped = true; } @Override @@ -325,4 +336,25 @@ return autocommit; } +// @Override +// public void injectServices(ServiceRegistryImplementor serviceRegistry) { +// this.serviceRegistry = serviceRegistry; +// } + + @Override + public boolean isUnwrappableAs(Class unwrapType) { + return ConnectionProvider.class.equals(unwrapType) || + getClass().isAssignableFrom(unwrapType); + } + + @Override + @SuppressWarnings({"unchecked"}) + public <T> T unwrap(Class<T> unwrapType) { + if (ConnectionProvider.class.equals(unwrapType) || + getClass().isAssignableFrom(unwrapType)) { + return (T) this; + } else { + throw new UnknownUnwrapTypeException(unwrapType); + } + } }
participants (1)
-
tchemit@users.nuiton.org