Java tutorial
/* * To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in * the editor. */ package de.freese.base.persistence.jdbc.driver; import static org.junit.Assert.assertTrue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import org.apache.commons.dbcp.BasicDataSource; import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.PoolProperties; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.springframework.jdbc.datasource.SingleConnectionDataSource; /** * @author Thomas Freese */ @RunWith(Parameterized.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestLoggingJdbcDriver { /** * @author Thomas Freese */ private static class BasicDataSourceConnectionPool implements IConnectionPool { /** * */ private final BasicDataSource dataSource; /** * Erstellt ein neues {@link BasicDataSourceConnectionPool} Object. */ public BasicDataSourceConnectionPool() { super(); this.dataSource = new BasicDataSource(); this.dataSource.setDriverClassName(DRIVER); this.dataSource.setUrl(URL); this.dataSource.setValidationQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"); this.dataSource.setUsername("sa"); this.dataSource.setPassword(null); this.dataSource.setMaxActive(5); this.dataSource.setMinIdle(1); this.dataSource.setMaxIdle(2); this.dataSource.setMaxWait(5000L); // nach 5 s. Exception werfen, wenn keine Connection verfgbar. this.dataSource.setTestOnBorrow(true); this.dataSource.setTestWhileIdle(true); this.dataSource.setTimeBetweenEvictionRunsMillis(1800000L); // 30 min. this.dataSource.setNumTestsPerEvictionRun(3); // Alle 10 min. Connection testen } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#close() */ @Override public void close() throws SQLException { this.dataSource.close(); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#getConnection() */ @Override public Connection getConnection() throws SQLException { return this.dataSource.getConnection(); } } /** * @author Thomas Freese */ private static class DriverManagerConnectionPool implements IConnectionPool { /** * Erstellt ein neues {@link DriverManagerConnectionPool} Object. */ public DriverManagerConnectionPool() { super(); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#close() */ @Override public void close() throws SQLException { // NOOP } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#getConnection() */ @Override public Connection getConnection() throws SQLException { return DriverManager.getConnection(URL); } } /** * @author Thomas Freese */ private static interface IConnectionPool { /** * @throws SQLException Falls was schief geht. */ public void close() throws SQLException; /** * @return {@link Connection} * @throws SQLException Falls was schief geht. */ public Connection getConnection() throws SQLException; } /** * @author Thomas Freese */ private static class SpringSingleConnectionDataSource implements IConnectionPool { /** * */ private final SingleConnectionDataSource dataSource; /** * Erstellt ein neues {@link SpringSingleConnectionDataSource} Object. */ public SpringSingleConnectionDataSource() { super(); this.dataSource = new SingleConnectionDataSource(); this.dataSource.setDriverClassName(DRIVER); this.dataSource.setUrl(URL); this.dataSource.setSuppressClose(true); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#close() */ @Override public void close() throws SQLException { this.dataSource.destroy(); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#getConnection() */ @Override public Connection getConnection() throws SQLException { return this.dataSource.getConnection(); } } /** * @author Thomas Freese */ private static class TomcatConnectionPool implements IConnectionPool { /** * */ private final DataSource dataSource; /** * Erstellt ein neues {@link TomcatConnectionPool} Object. */ public TomcatConnectionPool() { super(); PoolProperties poolProperties = new PoolProperties(); poolProperties.setDriverClassName(LoggingJdbcDriver.class.getName()); poolProperties.setUrl(URL); poolProperties.setUsername("sa"); poolProperties.setPassword(null); poolProperties.setTestWhileIdle(false); poolProperties.setTestOnBorrow(true); poolProperties.setTestOnReturn(false); poolProperties.setValidationQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"); poolProperties.setValidationInterval(1800000); poolProperties.setTimeBetweenEvictionRunsMillis(1800000); poolProperties.setMaxActive(2); poolProperties.setMaxIdle(2); poolProperties.setMinIdle(1); poolProperties.setInitialSize(1); poolProperties.setRemoveAbandoned(true); poolProperties.setLogAbandoned(true); poolProperties.setJmxEnabled(false); poolProperties.setJdbcInterceptors( "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); // removeAbandonedTimeout="10" this.dataSource = new DataSource(poolProperties); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#close() */ @Override public void close() throws SQLException { this.dataSource.close(); } /** * @see de.freese.base.persistence.jdbc.driver.TestLoggingJdbcDriver.IConnectionPool#getConnection() */ @Override public Connection getConnection() throws SQLException { return this.dataSource.getConnection(); } } /** * */ private static final String DRIVER = "org.hsqldb.jdbcDriver"; /** * */ private static final String URL = "jdbc:logger:jdbc:hsqldb:mem"; /** * @throws Exception Falls was schief geht. */ @BeforeClass public static void beforeClass() throws Exception { // Backend-Treiber: Wird durch App-Server oder Datasource erledigt. // Class.forName(DRIVER, true, ClassUtils.getDefaultClassLoader()); // Proxy-Treiber, bei Web-Anwendungen durch ServletContextListener erledigen oder durch Spring. DriverManager.registerDriver(new LoggingJdbcDriver()); LoggingJdbcDriver.addDefaultLogMethods(); // (System.setProperty("org.slf4j.simpleLogger.log.de.freese.base.persistence.jdbc.driver.LoggingJdbcDriver", "INFO"); } /** * @return {@link Iterable} * @throws Exception Falls was schief geht. */ @Parameters(name = "ConnectionPool: {0}") public static Iterable<Object[]> connectionPool() throws Exception { return Arrays.asList(new Object[][] { { "DriverManagerConnectionPool", new DriverManagerConnectionPool() }, { "SpringSingleConnectionDataSource", new SpringSingleConnectionDataSource() }, { "BasicDataSourceConnectionPool", new BasicDataSourceConnectionPool() }, { "TomcatConnectionPool", new TomcatConnectionPool() } }); } /** * */ private final IConnectionPool pool; /** * Erstellt ein neues {@link TestLoggingJdbcDriver} Object. * * @param name String * @param pool {@link IConnectionPool} */ public TestLoggingJdbcDriver(final String name, final IConnectionPool pool) { super(); this.pool = pool; } /** * @throws LException Falls was schief geht. */ @Test public void testDriver() throws Exception { // ("jdbc:logger:jdbc:hsqldb:file:D:/efreest/hsqldb/hsqldb;create=false;shutdown=true" // try (Connection connection = DriverManager.getConnection("jdbc:logger:jdbc:hsqldb:mem")) try (Connection connection = this.pool.getConnection()) { // try (Connection connection = dataSource.getConnection()) { try (PreparedStatement statement = connection .prepareStatement("select * from information_schema.tables where table_name like ?")) { statement.setString(1, "T%"); int i = 0; try (ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { // System.out.println(resultSet.getString("TABLE_NAME")); i++; } } assertTrue(i > 1); } } catch (Exception ex) { this.pool.close(); throw ex; } } }