与Java和Tomcat 7的基本数据库连接池 [英] Basic DB connection pool with Java and Tomcat 7

查看:146
本文介绍了与Java和Tomcat 7的基本数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  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;

公共类MySQLConnectionPool {

私有静态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(密码);
返回属性;
}

公共静态同步连接getConnection(){
连接连接= null;
try {
connection = datasource.getConnection();
} catch(SQLException ex){
System.out.println(从池中获取连接时出错!\ nSQL状态:+ ex.getSQLState()+\ nMESSAGE+ ex.getMessage());
}
返回连接;
}
}

我不确定静态属性,同步



我不确定池的客户端类。我知道他们只需要使用

 连接con = MySQLConnectionPool.getConnection(); 

并最终使用



$ b <$ p关闭此连接$ p> con.close();

这就是它?
而且,有没有更简单或更好的方法呢?



非常感谢!

解决方案

这是错误的做法。



Tomcat已经有一个连接池,您可以通过 context.xml 在没有任何代码的情况下配置和设置 conf 目录。



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



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



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

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

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


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.

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!

解决方案

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.

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.

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

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

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

这篇关于与Java和Tomcat 7的基本数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆