最近一直想自己封装下jdbc,写个orm,在此拿出来分享分享我的代码,让高人指点指点,共同进步,也做个备份
先上代码
DataSource.java
package org.tension.framework.common.das;
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 数据源
* @author tension
*
*/
public class DataSource {
protected static String driverClassName;
protected static String url;
protected static String user;
protected static String password;
protected static int poolSize;
protected static int maxUseCount;
protected static int maxPoolSize;
protected static int minPoolSize;
protected static String transaction_Isolation;
static{
try {
InputStream ips = DataSource.class.getClassLoader().getResourceAsStream("user-config.xml");
SAXReader reader = new SAXReader();
Document doc = null;
doc = reader.read(ips);
Element root = doc.getRootElement();
Element module = root.element("module");
Element group = module.element("group");
Iterator<?> configValue = group.elementIterator("configValue");
while(configValue.hasNext()){
Element e = (Element) configValue.next();
String key = e.attributeValue("key");
String value = e.getText();
if("DriverClass".equalsIgnoreCase(key)){
driverClassName = value;
}else if("Url".equalsIgnoreCase(key)){
url = value;
}else if("UserName".equalsIgnoreCase(key)){
user = value;
}else if("Password".equalsIgnoreCase(key)){
password = value;
}else if("PoolSize".equalsIgnoreCase(key)){
poolSize = Integer.valueOf(value);
}else if("MaxUseCount".equalsIgnoreCase(key)){
maxUseCount = Integer.valueOf(value);
}else if("MaxPoolSize".equalsIgnoreCase(key)){
maxPoolSize = Integer.valueOf(value);
}else if("MinPoolSize".equalsIgnoreCase(key)){
minPoolSize = Integer.valueOf(value);
}else if("Transaction-Isolation".equalsIgnoreCase(key)){
transaction_Isolation = value;
}
}
Class.forName(driverClassName);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}
至于为什么没有去实现DataSource接口,我觉得我暂时不用它,所有没有去实现
ConnectionPool.java
package org.tension.framework.common.das;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
/**
*
* 2012-9-12
*
* @author <a href="mailto:
[email protected]">tension</a>
*
*/
public class ConnectionPool extends DataSource{
/**当前连接数*/
int currentCount = 0;
LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
/**
* 默认构造方法,一次性获得poolSize个连接放进connectionsPool连接池中
*/
public ConnectionPool() {
CreateConnectionPool(poolSize);
}
/**
* 获取连接记录当前连接个数
* 如果连接池还有连接则直接从连接池取连接,如果连接大于最大连接数
* @return
* @throws SQLException
*/
public Connection getConnection() throws SQLException {
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0){
this.currentCount++;
return this.connectionsPool.removeFirst();
}
if(this.currentCount < maxPoolSize){
int buffer = this.currentCount + minPoolSize;
if (buffer < maxPoolSize || buffer == maxPoolSize) {
this.currentCount++;
CreateConnectionPool(minPoolSize);
}else{
this.currentCount++;
CreateConnectionPool(buffer - maxPoolSize);
}
return this.connectionsPool.removeFirst();
}
throw new SQLException("暂无连接可用");
}
}
/**
* 创建连接放入连接池中,缓冲连接池
* @param count 个数
*/
public void CreateConnectionPool(int count){
try {
for (int i = 0; i < count; i++) {
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* 释放连接方法
* 把用完的连接重新放回连接池中
* @param conn
*/
public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}
/**
* 创建连接
* @return warpedConnection
* @throws SQLException
* @author tension
*/
private Connection createConnection() throws SQLException {
Connection realConn = DriverManager.getConnection(url, user, password);
ConnectionHandler proxy = new ConnectionHandler(this);
return proxy.bind(realConn);
}
}
这个就是所谓的连接池了,简单,没有现在主流的那么NB,供学习
ConnectionHandler
package org.tension.framework.common.das;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
/**
* 连接池的代理类
* 用于处理Connection的colse方法,把连接放回连接池
* @author tension
* 2012-09-12
*/
class ConnectionHandler extends DataSource implements InvocationHandler{
/**真正的连接对象 */
private Connection realConnection;
/**代理的连接对象 */
private Connection warpedConnection;
/**连接池*/
private ConnectionPool conn;
/**当前用户使用的次数*/
private int currentUserCount = 0;
/**
* 默认构造方法
* @param conn 连接池
*/
ConnectionHandler(ConnectionPool conn) {
this.conn = conn;
}
/**
* java.sql.Connection的代理方法
* @param realConn warpedConnection 代理后的对象
* @return
*/
Connection bind(Connection realConn) {
this.realConnection = realConn;
this.warpedConnection = (Connection) Proxy.newProxyInstance(this
.getClass().getClassLoader(), new Class[] { Connection.class },
this);
return warpedConnection;
}
/**
* 方法拦截
* 如果为close方法的话不直接调用真正Connection的close方法
* 如果使用次数小于5次的话就放回连接池中否则关闭这个连接
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("close".equals(method.getName())) {
this.currentUserCount++;
if (maxUseCount == 0 || this.currentUserCount < maxUseCount)
this.conn.connectionsPool.addLast(this.warpedConnection);
else {
this.realConnection.close();
this.conn.currentCount--;
}
}
return method.invoke(this.realConnection, args);
}
}
Connection的代理,主要是修改colse方法
user-config.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<application>
<module name="DataSource">
<group name="default">
<configValue key="DriverClass">oracle.jdbc.driver.OracleDriver</configValue>
<configValue key="Url">jdbc:oracle:thin:@tension:1521:ORCL</configValue>
<configValue key="UserName">xxx</configValue>
<configValue key="Password">ooo</configValue>
<configValue key="PoolSize">1</configValue><!-- 初始大小 -->
<configValue key="MaxUseCount">0</configValue> <!-- 最大连接数 使用次数-->
<configValue key="MaxPoolSize">3</configValue> <!-- 最大连接数 -->
<configValue key="MinPoolSize">2</configValue><!-- 每次增长 -->
<configValue key="Transaction-Isolation">ISOLATION_READ_COMMITTED</configValue><!-- 事物隔离级别 -->
</group>
</module>
</application>