Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.Properties;
  11.  
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14.  
  15. public class DatabaseConnection {
  16.  
  17.     private static final ThreadLocal<Connection> con = new ThreadLocalConnection();
  18.     private final static Logger log = LoggerFactory.getLogger(DatabaseConnection.class);
  19.     private static Properties props = null;
  20.  
  21.     public static Connection getConnection() {
  22.         if (props == null) {
  23.             throw new RuntimeException("DatabaseConnection not initialized");
  24.         }
  25.         Connection conn = con.get();
  26.         try {
  27.             if (conn.isValid(1)) {
  28.                 return conn;
  29.             }
  30.         } catch (SQLException sqle) {
  31.             log.info("Lost connection to database, reconnecting...", sqle);
  32.             try {
  33.                 conn.close();
  34.             } catch (SQLException sqle2) {
  35.                 log.warn("Unable to close connection with database after losing connection", sqle2);
  36.             }
  37.         }
  38.         ThreadLocalConnection.allConnections.remove(conn);
  39.         con.remove();
  40.         return con.get();
  41.     }
  42.  
  43.     public static boolean isInitialized() {
  44.         return props != null;
  45.     }
  46.  
  47.     public static void setProps(Properties aProps) {
  48.         props = aProps;
  49.     }
  50.  
  51.     public static void cleanup(PreparedStatement ps, ResultSet rs) {
  52.         if (rs != null) {
  53.             try {
  54.                 rs.close();
  55.             } catch (SQLException ex) {
  56.                 ex.printStackTrace();
  57.             }
  58.         }
  59.         if (ps != null) {
  60.             try {
  61.                 ps.close();
  62.             } catch (SQLException ex) {
  63.                 ex.printStackTrace();
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void closeAll() throws SQLException {
  69.         for (Connection cons : ThreadLocalConnection.allConnections) {
  70.             cons.close();
  71.         }
  72.     }
  73.  
  74.     private static class ThreadLocalConnection extends ThreadLocal<Connection> {
  75.  
  76.         public static Collection<Connection> allConnections = new ArrayList<>();
  77.  
  78.         @Override
  79.         protected Connection initialValue() {
  80.             String driver = props.getProperty("driver");
  81.             String url = props.getProperty("url");
  82.             String user = props.getProperty("user");
  83.             String password = props.getProperty("password");
  84.             try {
  85.                 Class.forName(driver); // Touch the mysql driver.
  86.             } catch (ClassNotFoundException e) {
  87.                 log.error("ERROR", e);
  88.             }
  89.             try {
  90.                 Connection con = DriverManager.getConnection(url, user, password);
  91.                 allConnections.add(con);
  92.                 return con;
  93.             } catch (SQLException e) {
  94.                 log.error("ERROR", e);
  95.                 return null;
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement