Guest User

Untitled

a guest
Aug 3rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. Managing pooling datasource in Java web app with singleton class, Tomcat, C3P0
  2. import java.beans.PropertyVetoException;
  3. import java.sql.*;
  4. import javax.sql.*;
  5. import javax.naming.*;
  6. import com.mchange.v2.c3p0.*;
  7.  
  8. public class MyDataSource {
  9.  
  10. private static MyDataSource mds = new MyDataSource();
  11. public static DataSource ds;
  12.  
  13. private MyDataSource() {
  14. try {
  15. ds = getDataSource();
  16. } catch (NamingException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. public static MyDataSource getInstance(){
  22. return mds;
  23. }
  24.  
  25. public Connection getConnection() throws SQLException, NamingException {
  26. Connection myConnect = ds.getConnection();
  27. return myConnect;
  28. }
  29.  
  30. private DataSource getDataSource() throws NamingException {
  31.  
  32. ComboPooledDataSource cpds = new ComboPooledDataSource();
  33. try {
  34. cpds.setDriverClass( "com.mysql.jdbc.Driver" );
  35. } catch (PropertyVetoException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. cpds.setJdbcUrl( "jdbc:mysql://195.195.xx.xx:3306/dbName" );
  40. cpds.setUser("lemmy");
  41. cpds.setPassword("xxx");
  42. cpds.setMaxIdleTime(180);
  43. cpds.setMaxPoolSize(100);
  44. return cpds;
  45. }
  46. }
  47.  
  48. import java.sql.*;
  49. import javax.sql.*;
  50. import javax.naming.*;
  51.  
  52. public class DbConnection {
  53.  
  54. public Connection c;
  55.  
  56. public DbConnection() throws NamingException, SQLException {
  57. c = getConnection();
  58. }
  59.  
  60. public Connection getConnection() throws SQLException, NamingException {
  61. Connection myConnect = MyDataSource.getInstance().getConnection();
  62. return myConnect;
  63. }
  64.  
  65. public void close(){
  66. JDBCUtils.close(this.c);
  67. }
  68. }
  69.  
  70. import java.sql.*;
  71.  
  72. public class JDBCUtils {
  73.  
  74. static public void close (ResultSet rs) {
  75. try { if (rs!=null) rs.close(); } catch (Exception e) {}
  76. }
  77.  
  78. // Works for PreparedStatement also since it extends Statement.
  79. static public void close (Statement stmt) {
  80. try { if (stmt!=null) stmt.close(); } catch (Exception e) {}
  81. }
  82.  
  83. static public void close (java.sql.Connection conn) {
  84. try { if (conn!=null) conn.close(); } catch (Exception e) {}
  85. }
  86.  
  87. }
  88.  
  89. String myQuery = null;
  90. DbConnection myConnect = null;
  91. Statement myStatement = null;
  92. ResultSet rs = null;
  93.  
  94. try {
  95. myConnect = new DbConnection();
  96. myStatement = myConnect.c.createStatement();
  97.  
  98. // Do stuff here
  99.  
  100. }catch (SQLException e) {
  101. out.println("SQL Error: "+e);
  102. } finally {
  103. JDBCUtils.close(rs);
  104. JDBCUtils.close(myStatement);
  105. myConnect.close();
  106. }
Add Comment
Please, Sign In to add comment