Java 使用 apache dbcp 在 JDBC 中实现连接池

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20016046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:20:51  来源:igfitidea点击:

Implement Connection pooling in JDBC using apache dbcp

javamysqljdbcapache-commons-dbcp

提问by user2998826

Is this Good Code to implement pooling? I want to implement this in my project which has 30 threads operating concurrently and each thread requires more than four connection for each request? Does this code work?

这是实现池化的好代码吗?我想在我的项目中实现这一点,该项目有 30 个线程同时运行,并且每个线程每个请求需要四个以上的连接?这段代码有效吗?

    import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

/**
 *
 * @author
 * taher_JAVAHUNTER
 */

    public class JDBCHelper {

        private final static String username = "root";
        private final static String password = "";
        private final static String url = "jdbc:mysql://localhost:3306/treamisdemo";
        public static Connection connection = null;
        public static int connectionCount = 0;
    //    public JDBCHelper(boolean setCon) {
    //        try {
    //            setConnectionTest();
    //        } catch (Exception e) {
    //            System.out.println("Error in Connection:" + e.toString());
    //        }
    //    }
        public static BasicDataSource dataSource;

        public static Connection getConnection() throws SQLException {
            try {
                if (dataSource == null) {
                    dataSource = new BasicDataSource();
                    String driver = "com.mysql.jdbc.Driver";
                    try {
                        dataSource.setDriverClassName(driver);
                        dataSource.setUrl(url);
                        dataSource.setUsername(username);
                        dataSource.setPassword(password);
                        dataSource.setMaxActive(100);
                        dataSource.setMaxWait(10000);
                        dataSource.setMaxIdle(10);
                        if (connection == null || connection.isClosed()) {
                            System.out.println(" requeition CONNECTION WITH FIRST SERVER.");
                            connection = dataSource.getConnection();
                            connectionCount++;
                        }
                    } catch (SQLException e) {
                        System.out.println("***Connection Requisition*** Could not connect to the database msg :" + e.getMessage());
                    }
                } else {
                    connection = dataSource.getConnection();
                    connectionCount++;
                }
            } catch (Exception e) {
                System.out.println("open connection exception" + e);
            }
            return connection;
        }

        public static void close(ResultSet c) {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public static void close(Statement c) {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public static void close(Connection c) {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

}

回答by brettw

I would notrecommend this approach. You are basically creating a connection and hanging on it it. I'm not in love with your pattern, but something like this would be better:

不会推荐这种方法。您基本上是在创建一个连接并挂在它上面。我不喜欢你的模式,但这样的事情会更好:

public class DataTransaction {
   private final static BasicDataSource dataSource;

   static {
      dataSource = new BasicDataSource();
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/registrationtest");
      dataSource.setUsername("root");
      dataSource.setPassword("root");
      dataSource.setMaxActive(100);
      dataSource.setMaxWait(10000);
      dataSource.setMaxIdle(10);
   }

   private DataTransaction() {
   }

   public static DataSource getDataSource() {
      return dataSource;
   }
}

Further, I would not hardcode any of the DataSource parameters, but rather initialize the DataSource from a properties file.

此外,我不会对任何 DataSource 参数进行硬编码,而是从属性文件初始化 DataSource。