Author: athimel Date: 2014-04-03 19:07:52 +0200 (Thu, 03 Apr 2014) New Revision: 3045 Url: http://forge.nuiton.org/projects/topia/repository/revisions/3045 Log: refs #3139 Update TopiaConnectionProvider according to Hibernate 4.3.x Modified: branches/topia-2.9.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java branches/topia-2.9.x/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java Modified: branches/topia-2.9.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java =================================================================== --- branches/topia-2.9.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2014-04-03 16:59:58 UTC (rev 3044) +++ branches/topia-2.9.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2014-04-03 17:07:52 UTC (rev 3045) @@ -37,9 +37,12 @@ import org.hibernate.HibernateException; import org.hibernate.cfg.Environment; import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator; -import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.service.UnknownUnwrapTypeException; +import org.hibernate.service.spi.Configurable; +import org.hibernate.service.spi.Stoppable; /** * Customized connection provider. @@ -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 { /** 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,10 +108,12 @@ * 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; public TopiaConnectionProvider() { pool = new ArrayList<Connection>(); @@ -114,7 +121,6 @@ @Override public void configure(Map configurationValues) throws HibernateException { - String driverClass = (String)configurationValues.get(Environment.DRIVER); poolSize = ConfigurationHelper.getInt(Environment.POOL_SIZE, configurationValues, 20); //default pool size 20 if (log.isDebugEnabled()) { @@ -122,22 +128,24 @@ } autocommit = ConfigurationHelper.getBoolean(Environment.AUTOCOMMIT, configurationValues); - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("autocommit mode: " + autocommit); + } isolation = ConfigurationHelper.getInteger(Environment.ISOLATION, configurationValues); if (isolation != null) { if (log.isDebugEnabled()) { log.debug("JDBC isolation level: " + - Environment.isolationLevelToString(isolation)); + Environment.isolationLevelToString(isolation)); } } + String driverClass = ConfigurationHelper.getString(Environment.DRIVER, configurationValues); if (driverClass == null) { if (log.isWarnEnabled()) { log.warn("no JDBC Driver class was specified by property " + - Environment.DRIVER); + Environment.DRIVER); } } else { try { @@ -154,10 +162,9 @@ } } - 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; + String msg = "JDBC URL was not specified by property " + Environment.URL; if (log.isErrorEnabled()) { log.error(msg); } @@ -170,6 +177,7 @@ if (log.isDebugEnabled()) { log.debug("using driver: " + driverClass + " at URL: " + url); } + // if debug level is enabled, then log the password, otherwise mask it if (log.isTraceEnabled()) { log.debug("connection properties: " + connectionProps); @@ -192,7 +200,7 @@ int last = pool.size() - 1; if (log.isTraceEnabled()) { log.trace("using pooled JDBC connection, pool size: " + - last); + last); } connection = pool.remove(last); @@ -203,7 +211,7 @@ if (log.isDebugEnabled()) { log.debug("Remove already closed connection from pool " + - connection); + connection); } } } @@ -241,7 +249,7 @@ if (log.isDebugEnabled()) { log.debug("Connection [" + conn + - "] alreay closed!, will not use it any longer "); + "] alreay closed!, will not use it any longer "); } return; } @@ -254,7 +262,7 @@ if (currentSize < getPoolSize()) { if (log.isTraceEnabled()) { log.trace("returning connection to pool, pool size: " + - (currentSize + 1)); + (currentSize + 1)); } pool.add(conn); return; @@ -272,8 +280,10 @@ @Override protected void finalize() throws Throwable { + if (!stopped) { + stop(); + } super.finalize(); - stop(); } @Override @@ -293,7 +303,7 @@ } } pool.clear(); - + stopped = true; } @Override @@ -325,4 +335,20 @@ return autocommit; } + @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); + } + } } Modified: branches/topia-2.9.x/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java =================================================================== --- branches/topia-2.9.x/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2014-04-03 16:59:58 UTC (rev 3044) +++ branches/topia-2.9.x/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2014-04-03 17:07:52 UTC (rev 3045) @@ -28,7 +28,7 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; @@ -37,9 +37,12 @@ import org.hibernate.HibernateException; import org.hibernate.cfg.Environment; import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator; -import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.service.UnknownUnwrapTypeException; +import org.hibernate.service.spi.Configurable; +import org.hibernate.service.spi.Stoppable; /** * Customized connection provider. @@ -47,43 +50,67 @@ * @author tchemit <chemit@codelutin.com> * @since 2.5.3 */ -public class TopiaConnectionProviderHardCoded extends DriverManagerConnectionProviderImpl { +public class TopiaConnectionProviderHardCoded implements ConnectionProvider, Configurable, Stoppable { + private static final long serialVersionUID = 7911628440635459964L; + private String url; private Properties connectionProps; private Integer isolation; - private final ArrayList pool = new ArrayList(); + /** + * Our pool of connections which are not closed and availables. + */ + protected final List<Connection> pool; private int poolSize; + private boolean stopped; + private int checkedOut = 0; private boolean autocommit; - /** Logger. */ + /** + * Logger. + */ private static final Log log = LogFactory.getLog(TopiaConnectionProviderHardCoded.class); + public TopiaConnectionProviderHardCoded() { + pool = new ArrayList<Connection>(); + } + @Override public void configure(Map configurationValues) throws HibernateException { - String driverClass = (String)configurationValues.get(Environment.DRIVER); poolSize = ConfigurationHelper.getInt(Environment.POOL_SIZE, configurationValues, 20); //default pool size 20 - log.info("Using Hibernate built-in connection pool (not for production use!)"); - log.info("Hibernate connection pool size: " + poolSize); + if (log.isDebugEnabled()) { + log.debug("Connection pool size: " + poolSize); + } autocommit = ConfigurationHelper.getBoolean(Environment.AUTOCOMMIT, configurationValues); - log.info("autocommit mode: " + autocommit); + if (log.isDebugEnabled()) { + log.debug("autocommit mode: " + autocommit); + } isolation = ConfigurationHelper.getInteger(Environment.ISOLATION, configurationValues); - if (isolation != null) - log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation)); + if (isolation != null) { + if (log.isDebugEnabled()) { + log.debug("JDBC isolation level: " + + Environment.isolationLevelToString(isolation)); + } + } + String driverClass = ConfigurationHelper.getString(Environment.DRIVER, configurationValues); if (driverClass == null) { - log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER); + + if (log.isWarnEnabled()) { + log.warn("no JDBC Driver class was specified by property " + + Environment.DRIVER); + } } else { try { // trying via forName() first to be as close to DriverManager's semantics @@ -103,7 +130,7 @@ // the real directory where db is and then make sure hibernate always // use the connection provider... String directory = - (String)configurationValues.get(TopiaConnectionProviderTest.TEST_URL); + (String) configurationValues.get(TopiaConnectionProviderTest.TEST_URL); url = directory; // url = props.getProperty(Environment.URL); @@ -116,19 +143,24 @@ connectionProps = ConnectionProviderInitiator.getConnectionProperties(configurationValues); - log.info("using driver: " + driverClass + " at URL: " + url); - // if debug level is enabled, then log the password, otherwise mask it if (log.isDebugEnabled()) { - log.info("connection properties: " + connectionProps); - } else if (log.isInfoEnabled()) { - log.info("connection properties: " + ConfigurationHelper.maskOut(connectionProps, "password")); + log.debug("using driver: " + driverClass + " at URL: " + url); } + + // if debug level is enabled, then log the password, otherwise mask it + if (log.isTraceEnabled()) { + log.debug("connection properties: " + connectionProps); + } else if (log.isDebugEnabled()) { + log.debug("connection properties: " + + ConfigurationHelper.maskOut(connectionProps, "password")); + } } @Override public Connection getConnection() throws SQLException { - if (log.isTraceEnabled()) + if (log.isTraceEnabled()) { log.trace("total checked-out connections: " + checkedOut); + } synchronized (pool) { if (!pool.isEmpty()) { @@ -137,11 +169,13 @@ log.trace("using pooled JDBC connection, pool size: " + last); } checkedOut++; - Connection pooled = (Connection) pool.remove(last); - if (isolation != null) + Connection pooled = pool.remove(last); + if (isolation != null) { pooled.setTransactionIsolation(isolation.intValue()); - if (pooled.getAutoCommit() != autocommit) + } + if (pooled.getAutoCommit() != autocommit) { pooled.setAutoCommit(autocommit); + } return pooled; } } @@ -183,28 +217,51 @@ @Override protected void finalize() throws Throwable { + if (!stopped) { + stop(); + } super.finalize(); - close(); } - public void close() { + @Override + public void stop() { - log.info("cleaning up connection pool: " + url); + if (log.isDebugEnabled()) { + log.debug("cleaning up connection pool: " + url); + } - Iterator iter = pool.iterator(); - while (iter.hasNext()) { + for (Connection connection : pool) { try { - ((Connection) iter.next()).close(); + connection.close(); } catch (SQLException sqle) { - log.warn("problem closing pooled connection", sqle); + if (log.isWarnEnabled()) { + log.warn("problem closing pooled connection", sqle); + } } } pool.clear(); - + stopped = true; } @Override public boolean supportsAggressiveRelease() { return false; } + + @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); + } + } }