数据库链接池

http://www.2cto.com/kf/201407/317622.html

http://kamaila90.iteye.com/blog/968780

 

实现:

 1 package com.xmup.util;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.InputStream;
 5 import java.net.URL;
 6 import java.sql.Connection;
 7 import java.sql.DatabaseMetaData;
 8 import java.sql.SQLException;
 9 import java.util.Properties;
10 
11 import javax.sql.DataSource;
12 import org.apache.commons.dbcp.BasicDataSourceFactory;
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15 
16 /**
17  * tomcat dbcp 数据库连接池管理类<br>
18  * 使用为tomcat部署环境<br>
19  * 需要在类路径下准备数据库连接配置文件dbcp.properties
20  * 
21  * @author
22  */
23 public class DBConnPool {
24     private static final Logger Log = LogManager.getLogger();
25     
26     private DataSource dataSource = null;
27 
28     /*
29      * 
30      */
31     public DBConnPool(String cfgfile) {
32         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
33         if (classLoader == null) {
34             classLoader = DBConnPool.class.getClassLoader();
35         }
36         InputStream in = classLoader.getResourceAsStream(cfgfile);
37         URL url = classLoader.getResource(cfgfile);
38         
39         if (in == null) {
40             in = DBConnPool.class.getResourceAsStream(cfgfile);
41             url = DBConnPool.class.getResource(cfgfile);
42             if (in == null) {
43                 Log.error("DBConnPool: fail to load {}", cfgfile);
44             }
45         }
46         Log.info("load dbcp config from " + url);
47         
48         try {
49             Properties prop = new Properties();
50             BufferedInputStream reader = new BufferedInputStream(in);
51             prop.load(reader);
52             dataSource = BasicDataSourceFactory.createDataSource(prop);
53 
54             Connection conn = getConn();
55             if (conn != null) {
56                 DatabaseMetaData md = conn.getMetaData();
57                 Log.info("connected to {}", md.getDatabaseProductName());
58                         //md.getDatabaseProductVersion());
59                 closeConn(conn);
60             }
61             reader.close();
62         } catch (Exception e) {
63             Log.error("fail to connect to database {}\n", cfgfile, e);
64         }
65     }
66 
67     /**
68      * 获取链接,用完后记得关闭
69      * 
70      * @see {@link DBConnPool#closeConn(Connection)}
71      * @return
72      */
73     public Connection getConn() {
74         Connection conn = null;
75         try {
76             conn = dataSource.getConnection();
77         } catch (SQLException e) {
78             Log.error("fail to get db connection", e);
79         }
80         return conn;
81     }
82 
83     /**
84      * 关闭连接
85      * 
86      * @param conn,
87      *            需要关闭的连接
88      */
89     public void closeConn(Connection conn) {
90         try {
91             if (conn != null && !conn.isClosed()) {
92                 conn.setAutoCommit(true);
93                 conn.close();
94             }
95         } catch (SQLException e) {
96             Log.error("fail to close db connection", e);
97         }
98     }
99 }

 

posted @ 2015-11-16 23:40  i27oak  阅读(71)  评论(0)    收藏  举报