java 使用 c3p0 池连接数据库 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10999996/
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-10-31 03:21:52  来源:igfitidea点击:

connecting database mysql using c3p0 pooling

javamysqljndiconnection-poolingc3p0

提问by Arjun K P

ComboPooledDataSource cpds = new ComboPooledDataSource(); 
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver 

cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/dragon" ); 
cpds.setUser("root"); 
cpds.setPassword("password");
cpds.setMaxPoolSize(50);

I've created a java file containing the following code to configure a ComboPooledDataSource object. Now is this code enough to establish a pooled connection with the database?

我创建了一个包含以下代码的 java 文件来配置 ComboPooledDataSource 对象。现在这段代码足以建立与数据库的池连接吗?

If not, What else should I do ?

如果没有,我还应该做什么?

Also please tell how can I can implement JNDI here.

另外请告诉我如何在这里实现 JNDI。

Please explain it since I am a beginner.

由于我是初学者,请解释一下。

回答by Arjun K P

At First...Create the code to initiate the connection in a class containing static methods or variables like this the following..

首先...创建代码以在包含静态方法或变量的类中启动连接,如下所示..

        private static ComboPooledDataSource cpds = new ComboPooledDataSource();
        public static void MakePool()
        {
               try 
               {
                 cpds=new ComboPooledDataSource();
                 cpds.setDriverClass("com.mysql.jdbc.Driver");
                 cpds.setJdbcUrl("jdbc:mysql://localhost:3306/att_db");
                 cpds.setUser("root");
                 cpds.setPassword("dragon");
                 cpds.setMaxPoolSize(MaxPoolSize);
                 cpds.setMinPoolSize(MinPoolSize);
                 cpds.setAcquireIncrement(Accomodation);
             } 
             catch (PropertyVetoException ex) 
             {
                 //handle exception...not important.....
              }

}
public static Connection getConnection()
{
           return cpds.getConnection();
}

Once u r done Create another class meant for server operations....

完成后创建另一个用于服务器操作的类....

and get the Connections from the Pool...

并从池中获取连接...

         try{

               con=DatabasePool.getConnection();
               // DatabasePool is the name of the Class made earlier....
               .
               .
               .
               .  // Server operations as u wanted.....
               .
               .
             }
             catch(SQL EXCEPTION HERE)
             {
                  .....
             }
             finally
             {     
               if(con!=null)
               {
                      con.close();      
               }
             }

回答by janoside

I'm not familiar with JNDI so I won't address that (you probably want a separate question for that with more detail about your goal), but I believe you do have your ComboPooledDataSource configured properly.

我不熟悉 JNDI,所以我不会解决这个问题(您可能需要一个单独的问题,其中包含有关您的目标的更多详细信息),但我相信您确实正确配置了 ComboPooledDataSource。

You should be able to test your DataSource using code like this (exception handling excluded for simplicity):

您应该能够使用这样的代码来测试您的数据源(为简单起见,排除了异常处理):

ArrayList<Object[]> data = new ArrayList<Object[]>();

Connection connection = cpds.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select a_column from a_table;");

int columnCount = resultSet.getMetaData().getColumnCount();

while (resultSet.next()) {
    Object[] rowData = new Object[columnCount];

    for (int i = 0; i < columnCount; i++) {
        rowData[i] = resultSet.getObject(i + 1);
    }

    data.add(rowData);
}

回答by jiangwh

You use the c3p0 to manage the jdbc connection and should not use the jdni. If you want use the jndi , you need config the connection in the web container. Tomcat like

您使用 c3p0 来管理 jdbc 连接,不应使用 jdni。如果要使用 jndi ,则需要在 Web 容器中配置连接。像Tomcat一样

<Context>   
    <Resource name="jdbc/springflow" auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" username="root"
        password="" driverClassName="com.mysql.jdbc.Driver"     
        url="jdbc:mysql://localhost:3306/test" />
</Context>

And you can use jndi like context.lookup("java:jdbc/springflow").

你可以使用 jndi 之类的context.lookup("java:jdbc/springflow")