Advertisement
Guest User

Untitled

a guest
May 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import org.apache.logging.log4j.LogManager;
  2. import org.apache.logging.log4j.Logger;
  3. import org.h2.jdbcx.JdbcConnectionPool;
  4.  
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.util.Properties;
  10.  
  11. public class ConnectionManager {
  12.  
  13. private static final Logger log = LogManager
  14. .getLogger(ConnectionManager.class.getName());
  15.  
  16. private static volatile JdbcConnectionPool instance;
  17.  
  18. public static Connection getConnection() throws SQLException {
  19. log.traceEntry();
  20. log.traceExit();
  21. return getInstance().getConnection();
  22. }
  23.  
  24. public static JdbcConnectionPool getInstance() {
  25. log.traceEntry();
  26. JdbcConnectionPool localInstance = instance;
  27. if (localInstance == null) {
  28. synchronized (JdbcConnectionPool.class) {
  29. localInstance = instance;
  30. if (localInstance == null) {
  31. Properties properties = new Properties();
  32. try (InputStream input = getClass()
  33. .getResourceAsStream("/jdbc.properties")) {
  34. properties.load(input);
  35. } catch (IOException e) {
  36. log.catching(e);
  37. }
  38.  
  39. String url = properties.getProperty("jdbc.url");
  40. String username = properties.getProperty("jdbc.username");
  41. String password = properties.getProperty("jdbc.password");
  42.  
  43. instance = localInstance = JdbcConnectionPool
  44. .create(url, username, password);
  45. }
  46. }
  47. }
  48. log.traceExit();
  49. return localInstance;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement