日期:2014-05-16  浏览次数:20418 次

实现jdbcOpenConnectionInView
嘿嘿!看了看spring代码。自己写了个jdbc的连接
贴出来代码。

package com.jy.jdbc;

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

import javax.sql.DataSource;

import net.sourceforge.jtds.jdbcx.JtdsDataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConnectionFactory {
	private final static String USER = "";
	private final static String PWD = "";
	private final static String IP = "";
	private final static String DATABASE="";
	private final static Log logger = LogFactory.getLog(ConnectionFactory.class);
	private final static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
	private  static DataSource dst=null;
	// jtds数据源
	 static   {
		JtdsDataSource ds = new JtdsDataSource();
		ds.setServerType(1);// SQL SERVER
		ds.setServerName(IP);
		ds.setDatabaseName(DATABASE);
		ds.setPortNumber(1433);
		ds.setXaEmulation(true);
		try {
			ds.setSocketTimeout(3000);
		} catch (SQLException e1) {
			logger.error(e1);
		}
		ds.setMaxStatements(500);
		ds.setUser(USER);
		ds.setBatchSize(50);
		ds.setCacheMetaData(true);
		ds.setPassword(PWD);
		dst=ds;
	}
	 
	public static Connection getConnection() {
		Connection conn = threadLocal.get();
		if (conn == null) {
			try {
				conn = dst.getConnection();
				threadLocal.set(conn);
			} catch (SQLException e) {
				logger.error("从连接池获取连接失败!:" + e.getMessage());
			}
		}
		return conn;
	}
//dbcp连接池
//	public static DataSource getDataSource() {
//		BasicDataSource ds = new BasicDataSource();
//		ds.setDriverClassName(CLASSNAME);
//		ds.setUsername(USER);
//		ds.setPassword(PWD);
//		ds.setUrl(URL);
//		ds.setMaxActive(50);
//		ds.setMaxIdle(50);
//		ds.setMaxWait(3000); // 10 seconds
//		ds.setMaxOpenPreparedStatements(500);
//		System.out.println("NumActive: " + ds.getNumActive() + ","
//				+ "NumIdle: " + ds.getNumIdle());
//
//		return ds;
//	}

	


	// 关闭连接
	public static void closeConnection() {
		Connection conn = threadLocal.get();
		threadLocal.set(null);
		try {
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
		} catch (SQLException e) {
			logger.error("关闭连接失败!" + e.getMessage());
		}
	}


	public static void main(String[] args) {
		for (int i = 0; i < 2; i++) {
			Connection conn = getConnection();
			try {
				Statement st=conn.createStatement();
				ResultSet rs=st.executeQuery("select * from test");
				while(rs.next()){
					System.out.println(rs.getInt("id"));
				}
				closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}

写个request监听器
package com.tool;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import com.jinyu.jdbc.ConnectionFactory;
public class ListenterCloseConnection implements ServletRequestListener{

	public void requestDestroyed(ServletRequestEvent arg0) {
		ConnectionFactory.closeConnection();
	}

	public void requestInitialized(ServletRequestEvent arg0) {
		
	}


}


在web.xml
配置如下
<!-- 监听器,关闭Connection -->
<listener>
  <listener-class>
    com.tool.ListenterCloseConnection
  </listenerclass>
</listener>

那么同一次request请求,只打开一次连接!所以不论采用Struts还是Servlet还是直接用jsp。只打开连接。不需关闭连接。