Guest User

Untitled

a guest
Mar 30th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. package com.wkrzywiec.spring.library.config;
  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 LogsConnectionFactory {
  16.  
  17. private static interface Singleton {
  18. final LogsConnectionFactory INSTANCE = new LogsConnectionFactory();
  19. }
  20.  
  21. private final DataSource dataSource;
  22.  
  23. private String datasourceURL = "jdbc:mysql://localhost:3306/library_db?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  24. private String userName = "library-spring";
  25. private String pass = "library-spring";
  26.  
  27. private LogsConnectionFactory() {
  28.  
  29. Properties properties = new Properties();
  30. properties.setProperty("user", userName);
  31. properties.setProperty("password", pass);
  32.  
  33.  
  34. GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
  35. DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
  36. datasourceURL, properties
  37. );
  38.  
  39. new PoolableConnectionFactory(
  40. connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
  41. );
  42.  
  43. this.dataSource = new PoolingDataSource(pool);
  44. }
  45.  
  46. public static Connection getDatabaseConnection() throws SQLException {
  47. return Singleton.INSTANCE.dataSource.getConnection();
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment