com.alibaba.druid数据源工具类

Druid是阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好。它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser。 支持所有JDBC兼容的数据库,包括Oracle、MySQL、Derby、Postgresql、SQL Server、H2等等。 Druid针对oracle和mysql做了特别优化,比如Oracle的PS Cache内存占用优化,MySql的ping检测优化。Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,这是一个手写的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象语法树很方便。简单SQL语句用时10微秒以内,复杂SQL用时30微秒。 

Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息全部配置给DataSource对象。
1.配置文件db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://172.19.57.246:3306/webservice?useUnicode=true&characterEncoding=utf-8
username=root
password=root
filters=stat
initialSize=3
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

2.JdbcUtils工具类

package founder.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

/**
* @ClassName: JdbcUtils
* @author hanwl
* @date 2019年01月22日
* @Description: TODO
*/

public class JdbcUtils {
    
    // 工具类,私有化无参构造函数
    private JdbcUtils() {
    }
    private static JdbcUtils databasePool=null;
    private static DruidDataSource dataSource = null;
    
    // 静态代码块,加载配置文件。
    static{
        try{
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(in);            
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop);
            
        }catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    public static synchronized JdbcUtils getInstance() {
        if(null == databasePool){
            databasePool=new JdbcUtils();
        }
        return databasePool;
    }
    
    /**
     * 创建数据库连接实例
     * @return 数据库连接实例 connection
     */
    public DruidPooledConnection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("获取数据库连接异常");
    }
    
    /**
     * 关闭数据库连接实例
     */
    public static void releaseSqlConnection(ResultSet rSet, PreparedStatement pStatement,PreparedStatement iStatement, Connection connection) {
        try {
            if (rSet != null) {
                rSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pStatement != null) {
                    pStatement.close();
                }
                if(iStatement!=null){
                    iStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
 
 
原文地址:https://www.cnblogs.com/loong-hon/p/10315073.html