DataSources是有jdk中sql提供的接口供大家使用,可更快的实现数据库连接的获取,实现数据库连接池等功能;
- 使用动态代理的方式事项DataSource
package com.poweruniverse.app.job; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.LinkedList; import java.util.logging.Logger; import javax.sql.DataSource; public class LiYuDataBase implements DataSource{ private static LinkedList<Connection> pool=new LinkedList<Connection>(); private static final String driver="oracle.jdbc.OracleDriver"; private static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"; private static final String user="knowledge"; private static final String password="knowledge"; //初始化连接池 static{ try { Class.forName(driver); for(int i=0;i<100;i++) { Connection connection = DriverManager.getConnection(url, user, password); pool.add(connection); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } //使用代理对象获取数据库连接 @Override public Connection getConnection() throws SQLException { if(!pool.isEmpty()){ final Connection connection = pool.removeFirst(); return (Connection)Proxy.newProxyInstance(connection.getClass().getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() { @Override //proxy: 指代我们所代理的那个真实对象 //method: 指代的是我们所要调用真实对象的某个方法的Method对象 //args: 指代的是调用真实对象某个方法时接受的参数 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("close".equals(method.getName())){ return pool.add(connection); }else { return method.invoke(connection, args); } } }); }else { throw new RuntimeException("系统错误!!"); } } @Override public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } public static void main(String[] args) throws SQLException { LiYuDataBase db = new LiYuDataBase(); Connection connection = db.getConnection(); connection.close(); } }
- 使用Java包裹的方式事项DataSource
package com.poweruniverse.app.job;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;
import javax.sql.DataSource;
public class LiYuDataBaseT implements DataSource{
private static LinkedList<Connection> pool=new LinkedList<Connection>();
private static final String driver="oracle.jdbc.OracleDriver";
private static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static final String user="knowledge";
private static final String password="knowledge";
//采用包裹的方式实现数据源
public LiYuDataBaseT() {
for(int i=0;i<100;i++){
try {
Connection connection = DriverManager.getConnection(url);
pool.add(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Connection getConnection() throws SQLException {
return pool.size()>0?pool.removeFirst():null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
/**
* @Title: close
* @Description: TODO(<将连接添加到pool中等待下次再用>)
*/
public void close(Connection con){
pool.addLast(con);
}
}