连接池Druid的用法

Druid包括三部分: 

  • DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系。 
  • DruidDataSource 高效可管理的数据库连接池。 
  • SQLParser 

可以做什么? 

1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。 

2) 替换DBCP和C3P0。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。 

3) 数据库密码加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。 

4) SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。 

扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter-Chain机制,很方便编写JDBC层的扩展插件。

下面看个简单的例子:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
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;

/**
 * 
 * @author Administrator
 * 
 */
public class DbPoolConnection
{

	private static DbPoolConnection databasePool = null;
	private static DruidDataSource dds = null;
	static
	{
		Properties properties = loadPropertyFile("database.properties");
		try
		{
			dds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private DbPoolConnection()
	{
	}

	/**
	 * 
	 * @return
	 */
	public static synchronized DbPoolConnection getInstance()
	{
		if (null == databasePool)
		{
			databasePool = new DbPoolConnection();
		}
		return databasePool;
	}

	public DruidPooledConnection getConnection() throws SQLException
	{
		return dds.getConnection();
	}

	/**
	 * 
	 * @param fullFile
	 * @return
	 */
	private static Properties loadPropertyFile(String fullFile)
	{
		String webRootPath = null;
		if (null == fullFile || fullFile.equals(""))
			throw new IllegalArgumentException("Properties file path can not be null : " + fullFile);
		webRootPath = DbPoolConnection.class.getClassLoader().getResource("").getPath();
		InputStream inputStream = null;
		Properties p = null;
		try
		{
			inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile));
			p = new Properties();
			p.load(inputStream);
		} catch (FileNotFoundException e)
		{
			throw new IllegalArgumentException("Properties file not found: " + fullFile);
		} catch (IOException e)
		{
			throw new IllegalArgumentException("Properties file can not be loading: " + fullFile);
		} finally
		{
			try
			{
				if (inputStream != null)
					inputStream.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return p;
	}

}
#mysql数据库
url=jdbc:mysql://127.0.0.1:3306/android?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
#filters=stat
#最大连接数量
maxActive=20
#初始化连接数量
initialSize=10
#超时等待时间以毫秒为单位
maxWait=12000
#最小空闲连接
minIdle=5
#校验连接池中限制时间超过minEvictableIdleTimeMillis的连接对象
timeBetweenEvictionRunsMillis=6000
#连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒
#minEvictableIdleTimeMillis=
#SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前
validationQuery=SELECT now();
#指明连接是否被空闲连接回收器(如果有)进行检验.
#如果检测失败,则连接将被从池中去除.
testWhileIdle=true
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
testOnBorrow=false
#指明是否在归还到池中前进行检验
testOnReturn=false
#poolPreparedStatements=true
#maxPoolPreparedStatementPerConnectionSize=20


#-----------------------------------------------------------------
#详细配置请参考:http://blog.csdn.net/yunnysunny/article/details/8657095
#-----------------------------------------------------------------
   我的博客其他文章列表      
http://my.oschina.net/helu  




你可能感兴趣的:(连接池Druid的用法)