Guest User

Untitled

a guest
Jun 3rd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class Conn implements Serializable {
  2.  
  3.  
  4.  
  5. private static ComboPooledDataSource cpds;
  6.  
  7. protected transient Connection conn;
  8.  
  9.  
  10.  
  11. public void connect() {
  12.  
  13. conn = Conn.getConnection();
  14.  
  15. }
  16.  
  17.  
  18.  
  19. public static Connection getConnection() {
  20.  
  21. Connection ret = null;
  22.  
  23. if (cpds == null) {
  24.  
  25. createPool();
  26.  
  27. }
  28.  
  29. try {
  30.  
  31. ret = cpds.getConnection();
  32.  
  33. } catch (SQLException e) {
  34.  
  35. // <skip> some code
  36.  
  37. }
  38.  
  39. return retorno;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. public void disconnect() {
  46.  
  47. try {
  48.  
  49. conn.close();
  50.  
  51. } catch (SQLException e) {
  52.  
  53. // <skip> some code
  54.  
  55. }
  56.  
  57. }
  58.  
  59.  
  60.  
  61. private static void createPool() {
  62.  
  63. cpds = new ComboPooledDataSource();
  64.  
  65. cpds.setDriverClass("com.mysql.jdbc.Driver");
  66.  
  67. cpds.setJdbcUrl("jdbc:mysql://host/db?user=..?password=...");
  68.  
  69. cpds.setUser("user");
  70.  
  71. cpds.setPassword("pwd");
  72.  
  73. cpds.setAcquireIncrement(2);
  74.  
  75. cpds.setMaxConnectionAge(60 * 60 * 3);
  76.  
  77. cpds.setMaxIdleTimeExcessConnections(60 * 10);
  78.  
  79. cpds.setPreferredTestQuery("SELECT 1");
  80.  
  81. cpds.setIdleConnectionTestPeriod(60 * 10);
  82.  
  83. cpds.setMinPoolSize(3);
  84.  
  85. cpds.setMaxPoolSize(30);
  86.  
  87. }
  88.  
  89.  
  90.  
  91. }
Add Comment
Please, Sign In to add comment