Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package TestPool;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import org.apache.commons.dbcp2.BasicDataSource;
  8.  
  9. public class TestConnPool {
  10.  
  11. /**
  12. * @param args the command line arguments
  13. */
  14. public static void main(String[] args) {
  15. Connection connection = null;
  16. PreparedStatement statement = null;
  17. ResultSet resultSet = null;
  18. try {
  19. BasicDataSource bds = DataSource.getInstance().getBds();
  20. connection = bds.getConnection();
  21. statement = connection
  22. .prepareStatement("select id from TestConnPool;");
  23. resultSet = statement.executeQuery();
  24. while (resultSet.next()) {
  25. System.out.println(resultSet.getString("id"));
  26. }
  27.  
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. if (resultSet != null) {
  33. resultSet.close();
  34. }
  35. if (statement != null) {
  36. statement.close();
  37. }
  38. if (connection != null) {
  39. connection.close();
  40. }
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. }
  47. }
  48.  
  49. package TestPool;
  50.  
  51. import org.apache.commons.dbcp2.BasicDataSource;
  52.  
  53. public class DataSource {
  54.  
  55. private static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
  56. private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
  57. private static final String DB_USER = "root";
  58. private static final String DB_PASSWORD = "root";
  59. private static final int CONN_POOL_SIZE = 50;
  60. private static final int MIN_CONN_POOL_SIZE = 30;
  61. private static final int MAX_CONN_POOL_SIZE = 45;
  62.  
  63. private BasicDataSource bds = new BasicDataSource();
  64.  
  65. private DataSource() {
  66. //Set database driver name
  67. bds.setDriverClassName(DRIVER_CLASS_NAME);
  68. //Set database url
  69. bds.setUrl(DB_URL);
  70. //Set database user
  71. bds.setUsername(DB_USER);
  72. //Set database password
  73. bds.setPassword(DB_PASSWORD);
  74. //Set the connection pool size
  75. bds.setInitialSize(CONN_POOL_SIZE);
  76. //Set the connection minimum pool size
  77. bds.setMinIdle(MIN_CONN_POOL_SIZE);
  78. //Set the connection maximum pool size
  79. bds.setMaxTotal(MAX_CONN_POOL_SIZE);
  80.  
  81. }
  82.  
  83. public static class DataSourceHolder {
  84. private static final DataSource INSTANCE = new DataSource();
  85. }
  86.  
  87. public static DataSource getInstance() {
  88. return DataSourceHolder.INSTANCE;
  89. }
  90.  
  91. public BasicDataSource getBds() {
  92. return bds;
  93. }
  94.  
  95. public void setBds(BasicDataSource bds) {
  96. this.bds = bds;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement