Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.util.Properties;
  6.  
  7. import javax.sql.DataSource;
  8.  
  9. import org.apache.commons.dbcp.DriverManagerConnectionFactory;
  10. import org.apache.commons.dbcp.PoolableConnection;
  11. import org.apache.commons.dbcp.PoolableConnectionFactory;
  12. import org.apache.commons.dbcp.PoolingDataSource;
  13. import org.apache.commons.pool.impl.GenericObjectPool;
  14.  
  15. public class ConnectionFactory {
  16. private static interface Singleton {
  17. final ConnectionFactory INSTANCE = new ConnectionFactory();
  18. }
  19.  
  20. private final DataSource dataSource;
  21.  
  22. private ConnectionFactory() {
  23. Properties properties = new Properties();
  24. properties.setProperty("user", "root");
  25. properties.setProperty("password", "tiger");
  26.  
  27. GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
  28. DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
  29. "jdbc:mysql://127.0.0.1/logging", properties);
  30. new PoolableConnectionFactory(connectionFactory, pool, null,
  31. "SELECT 1",3, false, false,
  32. Connection.TRANSACTION_READ_COMMITTED
  33. );
  34.  
  35. this.dataSource = new PoolingDataSource(pool);
  36. }
  37.  
  38. public static Connection getDatabaseConnection() throws SQLException {
  39. return Singleton.INSTANCE.dataSource.getConnection();
  40. }
  41. }
  42.  
  43. <dependency>
  44. <groupId>commons-pool</groupId>
  45. <artifactId>commons-pool</artifactId>
  46. <version>1.6</version>
  47. </dependency>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement