与Java和Tomcat 7的基本数据库连接池

时间:2022-04-23 17:00:34

I'm trying to create my first connection pool. I'm creating a Java web aplication with Tomcat 7 and a MySQL DB, and I'd like to create the simplest connection pool possible. I've taken a look at several tutorials but it's not really clear for me, so I'd like you to confirm if I'm doing well.

我正在尝试创建我的第一个连接池。我正在使用Tomcat 7和MySQL DB创建Java Web应用程序,我想创建最简单的连接池。我已经看过几个教程,但对我来说并不是很清楚,所以我想让你确认我是否做得很好。

I've written the following class as a connection pool manager:

我已将以下类编写为连接池管理器:

package dao.mysql;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class MySQLConnectionPool {

    private static DataSource datasource;
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/mydb";
    private static String username = "user";
    private static String password = "password";

    public MySQLConnectionPool() {
        datasource = new DataSource(configurePoolProperties(driver, url, username, password));
    }

    private PoolProperties configurePoolProperties(String driver, String url, String username, String password) {
        PoolProperties properties = new PoolProperties();
        properties.setDriverClassName(driver);
        properties.setUrl(url);
        properties.setUsername(username);
        properties.setPassword(password);
        return properties;
    } 

    public static synchronized Connection getConnection() {
        Connection connection = null;
        try {
            connection = datasource.getConnection();
        } catch (SQLException ex) {
            System.out.println("Error while getting a connection from the pool! \nSQL state:" + ex.getSQLState() + "\nMESSAGE" + ex.getMessage());
        }
        return connection;
    }
}

I'm not sure about the static properties nor the synchronized.

我不确定静态属性和同步。

And I'm not sure about the "client" classes of the pool. I understand they have only to get a connection using

而且我不确定池中的“客户端”类。我知道他们只需要使用连接

Connection con = MySQLConnectionPool.getConnection();

and finally close this connection using

最后使用关闭此连接

con.close();

And that's it? And also, is there any simpler or better way to do this?

就是这样吗?而且,有没有更简单或更好的方法来做到这一点?

Thanks very much!

非常感谢!

2 个解决方案

#1


18  

This is the wrong way to do it.

这是错误的做法。

Tomcat already has a connection pool and you can configure and setup without any code through the context.xml in the conf directory.

Tomcat已经有一个连接池,您可以通过conf目录中的context.xml配置和设置,而无需任何代码。

Once it is defined there, all you need to do is to lookup the JNDI DataSource in your code. Hardcoding all that (and re-inventing the wheel) is a very bad idea.

在那里定义之后,您需要做的就是在代码中查找JNDI DataSource。硬编码所有这些(并重新发明*)是一个非常糟糕的主意。

To learn how to configure a JNDI DataSource check out the manual: http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

要了解如何配置JNDI数据源,请查看手册:http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

The Tomcat manual also has an example on how to obtain a connection from the pool:

Tomcat手册还有一个如何从池中获取连接的示例:

InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/dsname" );

where dsname is the name you provided in the context.xml

其中dsname是您在context.xml中提供的名称

#2


1  

Check out the JNDI Datasource HOW-TO and the Tomcat JDBC Connection Pool Tomcat documentation. Letting Tomcat do it is preferable especially since it avoids class loader leaks.

查看JNDI数据源HOW-TO和Tomcat JDBC连接池Tomcat文档。让Tomcat这样做是特别优选的,因为它避免了类加载器泄漏。

#1


18  

This is the wrong way to do it.

这是错误的做法。

Tomcat already has a connection pool and you can configure and setup without any code through the context.xml in the conf directory.

Tomcat已经有一个连接池,您可以通过conf目录中的context.xml配置和设置,而无需任何代码。

Once it is defined there, all you need to do is to lookup the JNDI DataSource in your code. Hardcoding all that (and re-inventing the wheel) is a very bad idea.

在那里定义之后,您需要做的就是在代码中查找JNDI DataSource。硬编码所有这些(并重新发明*)是一个非常糟糕的主意。

To learn how to configure a JNDI DataSource check out the manual: http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

要了解如何配置JNDI数据源,请查看手册:http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

The Tomcat manual also has an example on how to obtain a connection from the pool:

Tomcat手册还有一个如何从池中获取连接的示例:

InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/dsname" );

where dsname is the name you provided in the context.xml

其中dsname是您在context.xml中提供的名称

#2


1  

Check out the JNDI Datasource HOW-TO and the Tomcat JDBC Connection Pool Tomcat documentation. Letting Tomcat do it is preferable especially since it avoids class loader leaks.

查看JNDI数据源HOW-TO和Tomcat JDBC连接池Tomcat文档。让Tomcat这样做是特别优选的,因为它避免了类加载器泄漏。