从零实现MVC框架之数据库连接池(2)

来源:互联网 发布:预制梁悬臂参数c算法 编辑:程序博客网 时间:2024/06/05 17:31


使用mysql数据库,JDK1.7。

数据库配置

 

config_db.properties文件放到src下,如下:

#数据库配置driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8username=***password=*****poolsize=20autocommit=true


读取配置的类,如下:

package com.hc.pool;import java.io.FileInputStream;import java.io.IOException;import java.util.Properties;/** *  * @author chuer * @date 2014-7-15 下午3:35:37 * @version V1.0 */public class DBConfig {public static  int MAX_POOL_SIZE = 20;public static  String URL = "";public static  String DRIVER = "";public static  String USERNAME = "";public static  String PASSWORD = "";public static boolean AUTOCOMMIT = true;static{//读取配置文件Properties prop = new Properties();try {String path = DBConfig.class.getResource("/").getPath();prop.load(new FileInputStream(path+"config_db.properties"));DRIVER = (String)prop.get("driver");URL = (String)prop.get("url");USERNAME = (String)prop.get("username");PASSWORD = (String)prop.get("password");AUTOCOMMIT = Boolean.parseBoolean(prop.get("autocommit").toString());MAX_POOL_SIZE = Integer.parseInt(prop.get("poolsize").toString());} catch (IOException e) {e.printStackTrace();}}public static void main(String...args){}}


连接管理类

代码如下:

package com.hc.pool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** *  * @author chuer * @date 2014-7-15 下午3:35:37 * @version V1.0 */public class DBManager {public static void initPool(){try {Class.forName(DBConfig.DRIVER);for(int i=0;i<DBConfig.MAX_POOL_SIZE;i++){Connection connection = getConnection();connection.setAutoCommit(false);DBPool.getInstance().addConnection(connection);}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}public static Connection getConnection() {Connection conn = null;try {conn = DriverManager.getConnection(DBConfig.URL, DBConfig.USERNAME, DBConfig.PASSWORD);} catch (SQLException e) {e.printStackTrace();}return conn;}}


 

连接池

接口:

package com.hc.core;import java.sql.Connection;/** *  * @author chuer * @date 2014-7-15 下午6:40:24 * @version V1.0 */public interface IPool {public Connection getConnection();public void relaseConnection(Connection conn);}


 

实现类:

package com.hc.pool;import java.sql.Connection;import java.sql.SQLException;import java.util.concurrent.ConcurrentLinkedQueue;import com.hc.core.IPool;/** *  * @author chuer * @date 2014-7-15 下午3:35:37 * @version V1.0 */public class DBPool implements IPool{private static volatile boolean isInit = false;private DBPool(){}private static DBPool dbPool = new DBPool();private ConcurrentLinkedQueue<Connection> pools = new ConcurrentLinkedQueue<Connection>();public static DBPool getInstance(){if(!isInit){isInit = true;dbPool.init();}return dbPool;}public void init(){DBManager.initPool();}@Overridepublic Connection getConnection() {Connection conn = pools.poll();if(conn == null){conn = DBManager.getConnection();}return conn;}@Overridepublic void relaseConnection(Connection conn) {try {if(!conn.isClosed()){pools.offer(conn);}else{Connection connection = DBManager.getConnection();pools.offer(connection);}} catch (SQLException e) {e.printStackTrace();}}void addConnection(Connection conn){pools.offer(conn);}}

测试

package com.hc.pool;import java.sql.Connection;import java.sql.SQLException;public class DBTest {/** * @param args * @throws SQLException  */public static void main(String[] args) throws SQLException {for(int i=1;i<=40;i++){//取得连接Connection connection = DBPool.getInstance().getConnection();//业务处理//System.out.println(connection.isClosed());System.out.println(connection+"   "+i);//释放连接DBPool.getInstance().relaseConnection(connection);}}}


运行测试类,输出如下:

com.mysql.jdbc.JDBC4Connection@14174f9   1com.mysql.jdbc.JDBC4Connection@669255   2com.mysql.jdbc.JDBC4Connection@114a41   3com.mysql.jdbc.JDBC4Connection@1220b15   4com.mysql.jdbc.JDBC4Connection@5966f   5com.mysql.jdbc.JDBC4Connection@d76d1e   6com.mysql.jdbc.JDBC4Connection@13ce184   7com.mysql.jdbc.JDBC4Connection@1f9cdda   8com.mysql.jdbc.JDBC4Connection@139d369   9com.mysql.jdbc.JDBC4Connection@166b0df   10com.mysql.jdbc.JDBC4Connection@e62a39   11com.mysql.jdbc.JDBC4Connection@de1b36   12com.mysql.jdbc.JDBC4Connection@1cc946b   13com.mysql.jdbc.JDBC4Connection@2423ad   14com.mysql.jdbc.JDBC4Connection@a26638   15com.mysql.jdbc.JDBC4Connection@1e4e6db   16com.mysql.jdbc.JDBC4Connection@f0474c   17com.mysql.jdbc.JDBC4Connection@d8fb2b   18com.mysql.jdbc.JDBC4Connection@151e135   19com.mysql.jdbc.JDBC4Connection@9875d1   20com.mysql.jdbc.JDBC4Connection@14174f9   21com.mysql.jdbc.JDBC4Connection@669255   22com.mysql.jdbc.JDBC4Connection@114a41   23com.mysql.jdbc.JDBC4Connection@1220b15   24com.mysql.jdbc.JDBC4Connection@5966f   25com.mysql.jdbc.JDBC4Connection@d76d1e   26com.mysql.jdbc.JDBC4Connection@13ce184   27com.mysql.jdbc.JDBC4Connection@1f9cdda   28com.mysql.jdbc.JDBC4Connection@139d369   29com.mysql.jdbc.JDBC4Connection@166b0df   30com.mysql.jdbc.JDBC4Connection@e62a39   31com.mysql.jdbc.JDBC4Connection@de1b36   32com.mysql.jdbc.JDBC4Connection@1cc946b   33com.mysql.jdbc.JDBC4Connection@2423ad   34com.mysql.jdbc.JDBC4Connection@a26638   35com.mysql.jdbc.JDBC4Connection@1e4e6db   36com.mysql.jdbc.JDBC4Connection@f0474c   37com.mysql.jdbc.JDBC4Connection@d8fb2b   38com.mysql.jdbc.JDBC4Connection@151e135   39com.mysql.jdbc.JDBC4Connection@9875d1   40


 

从输出我们看到,1-20  和 21-40的对象是相等的。因为我们连接池的数量是20。需要注意的是getConnection方法和relaseConnection方法是成对出现的。从连接池取得的连接必须释放。


0 0
原创粉丝点击