>>分享Java Web开发技术,并且对孙卫琴的《Tomcat与Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 23749 个阅读者 刷新本主题
 * 贴子主题:  在web应用中使用数据库连接池 回复文章 点赞(0)  收藏  
作者:sunshine    发表时间:2017-08-01 14:15:08     消息  查看  搜索  好友  邮件  复制  引用

一、让数据库连接池转起来

作为一个JavaEE程序员大家手上可能会有现成的JDBC 数据库连接池,其实这没有太大的必要,因为象weblogic……企业级WEBAPP都有自己的连接池,大家不要费力直接使用吧,效率也很不错,再也不用羡慕.NET的ADO了(以前作MS从来不担心数据连接,ADO确实用起来很爽),如果想实现一个 JDBC connection pool 的注意事项有:

1. 有一个简单的函数从连接池中得到一个 Connection。

2. close 函数必须将 connection 放回 数据库连接池。

3. 当数据库连接池中没有空闲的 connection, 数据库连接池必须能够自动增加 connection 个数。

4. 当数据库连接池中的 connection 个数在某一个特别的时间变得很大,但是以后很长时间只用其中一小部分,应该可以自动将多余的 connection 关闭掉。

5. 如果可能,应该提供debug 信息报告没有关闭的 new Connection 。

网上有各种各样的连接池代码,抄过来改改吧,嘿嘿~



这里介绍如何配置TOMCAT的连接池,以SQLSERVER为例:

步骤1:安装SQLSERVER的JDBC驱动

SQLSERVER的JDBC驱动其实就是三个JAR文件,msbase.jar/mssqlserver.jar/msutil.jar,将这三个文件拷贝到你的/tomcat_home/common/lib目录下去就可以了。

步骤2:修改server.xml文件

具体代码如下:

<Context path="test" docBase="F:\yourroot" debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/>

<Resource name="jdbc/SqlServerDB" auth="Container" type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/SqlServerDB">

<parameter>

<name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

</parameter>

<!-- Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit.-->

<parameter>

<name>maxActive</name>

<value>50</value>

</parameter>

<!-- Maximum number of idle dB connections to retain in pool. Set to 0 for no limit.-->

<parameter>

<name>maxIdle</name>

<value>20</value>

</parameter>

<!-- Maximum time to wait for a dB connection to become available in ms, in this example 0.5 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely. -->

<parameter>

<name>maxWait</name>

<value>500</value>

</parameter>

<!-- msSQL dB username and password for dB connections -->

<parameter>

<name>username</name>

<value>sa</value>

</parameter>



<parameter>

<name>password</name>

<value>sa</value>

</parameter>

<!-- Class name for SQLServer2000 JDBC driver -->

<parameter>

<name>driverClassName</name>

<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>

</parameter>

<!-- The JDBC connection url for connecting to your MS SQL Server dB.The autoReconnect=true argument to the url makes sure that the mm.Sql Server JDBC Driver will automatically reconnect if mysqld closed the connection. mysqld by default closes idle connections after 8 hours.-->

<parameter>

<name>url</name>

<value>jdbc:microsoft:sqlserver://10.0.254.11:1433;databaseName=yourdb</value>

<!--must use & not use & -->

</parameter>

</ResourceParams>

</Context>

步骤三:程序调用

package dbmanage;



import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Vector;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;



import util.smartDateFormat;





public class dbManager {

/************************************

* @param static private boolean VERBOSE ;

* @param Statement theStatement;

* @param PreparedStatement thePstmt;

* @param Connection theConnection;

************************************/

final static private boolean VERBOSE = true; //打印控制台控制



//static Logger logger = Logger.getLogger(dbManager.class.getName());

private Context initCtx = null;

private Context ctx = null;

private DataSource ds = null;



private long timeout = 5000;



private Statement theStatement = null;

private PreparedStatement thePstmt = null;



/************************************

* 初试化initCtx

* 取得数据源对象

************************************/

public

dbManager() {

try {

initCtx = new InitialContext();

//init context,read config web.xml

if (initCtx == null) {

throw new Exception("Initial Failed!");

}

ctx = (Context) initCtx.lookup("java:comp/env");

//find "jdbc/SqlServerDB" object this configruation in the SERVER.XML of Tomcat

if (ctx != null) {

ds = (DataSource) ctx.lookup("jdbc/SqlServerDB");

}

if (ds == null) {

throw new Exception("Look up DataSource Failed!");

}

}

catch (Exception e) {

log(e, "Can't get the Context!");

}

}



/************************************

* get Connection

* @return Connection

************************************/

public synchronized

Connection getConnection() {

//get connection and set to delay time

long startTime = new java.util.Date().getTime();

Connection con = null;

while (con == null) {

con = newConnection();

if (con != null) {

//log("Create New Connection!");

break;

}

try {

log("连接超时,重新连接,等待" + timeout + "ms");

wait(timeout);

}

catch (InterruptedException e) {

log(e, "连接超时!");

}

if ( (new java.util.Date().getTime() - startTime) >= timeout) {

log("Connection timeout!");

break;

}

}

return con;

}



private

Connection newConnection() {

Connection con = null;

try {

con = ds.getConnection();

if (con == null) {

throw new Exception("Create Connection Failed!");

}

}

catch (Exception e) {

log("Create Connection Failed!");

System.out.println(e.getMessage());

}

return con;

}



/************************************

* release the connection

* @param conn Connection

* @param stmt Statement

* @param pstmt PreparedStatement

************************************/

public synchronized

void freeConnection(Connection conn,

Statement stmt,

PreparedStatement pstmt) {

try {

//close Statement

if (stmt != null) {

stmt.close();

stmt = null;

//log("Close Statement......");

}

//close PreparedStatement

if (pstmt != null) {

pstmt.close();

pstmt = null;

//log("Close PreparedStatement......");

}

}

catch (Exception e) {

System.out.println(e.getMessage());

}

try {

//close Connection

if (conn != null) {

conn.close();

conn = null;

//log("Close Connection......");

}

}

catch (SQLException e) {

log(e, "释放资源出错!");

}

}





/************************************

* write log file.

* @param s String

************************************/

private

void log(String s) {

if (VERBOSE) {

System.out.println(new java.util.Date() + ":" + s);

//logger.info(new java.util.Date()+s);

}

}



/************************************

* write log file.

* @param ex Object

************************************/

private

void logerr(Object ex) {

if (VERBOSE) {

//System.out.println(new java.util.Date()+":"+s);

//logger.error(ex);

}

}



/************************************

* write log file.

* @param e Throwable

* @param msg String

************************************/

private

void log(Throwable e, String msg) {

System.out.println(new java.util.Date() + ": " + msg);

//logger.info(new java.util.Date() + ": " + msg, e);

}

……

}



OK,你现在可以方便的使用连接池了,记得要释放每个连接哦,连接池的数量总是有限的。



程序猿的技术大观园:www.javathinker.net
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=313

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员