import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.naming.InitialContext; import javax.sql.DataSource; public class MyServletJspOrEjb { public void doSomething() throws Exception { /* * Create a JNDI Initial context to be able to * lookup the DataSource ** In production-level code, this should be cached as * an instance or static variable, as it can * be quite expensive to create a JNDI context. ** Note: This code only works when you are using servlets * or EJBs in a J2EE application server. If you are * using connection pooling in standalone Java code, you * will have to create/configure datasources using whatever * mechanisms your particular connection pooling library * provides. */ InitialContext ctx = new InitialContext(); /* * Lookup the DataSource, which will be backed by a pool * that the application server provides. DataSource instances * are also a good candidate for caching as an instance * variable, as JNDI lookups can be expensive as well. */ DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB"); /* * The following code is what would actually be in your * Servlet, JSP or EJB 'service' method...where you need * to work with a JDBC connection. */ Connection conn = null; Statement stmt = null; try { conn = ds.getConnection(); /* * Now, use normal JDBC programming to work with * MySQL, making sure to close each resource when you're * finished with it, which allows the connection pool * resources to be recovered as quickly as possible */ stmt = conn.createStatement(); stmt.execute("SOME SQL QUERY"); stmt.close(); stmt = null; conn.close(); conn = null; } finally { /* * close any jdbc instances here that weren't * explicitly closed during normal code path, so * that we don't 'leak' resources... */ if (stmt != null) { try { stmt.close(); } catch (sqlexception sqlex) { // ignore -- as we can't do anything about it here } stmt = null; } if (conn != null) { try { conn.close(); } catch (sqlexception sqlex) { // ignore -- as we can't do anything about it here } conn = null; } } } } 是不是通过类似上面的代码,不用配置服务器也能设置数据库连接池啊
我说的不是在xml里写代码,我想问的就是上面的代码是什么作用
public class DBConnectionPool implements TimerListener{ private int checkedOut;//已被分配出去的连接数 private ArrayList freeConnections = new ArrayList();//容器,空闲池,根据//创建时间顺序存放已创建但尚未分配出去的连接 private int minConn;//连接池里连接的最小数量 private int maxConn;//连接池里允许存在的最大连接数 private String name;//为这个连接池取个名字,方便管理 private String password;//连接数据库时需要的密码 private String url;//所要创建连接的数据库的地址 private String user;//连接数据库时需要的用户名 public Timer timer;//定时器 public DBConnectionPool(String name, String URL, String user, String password, int maxConn)//公开的构造函数 public synchronized void freeConnection(Connection con) //使用完毕之后,//把连接返还给空闲池 public synchronized Connection getConnection(long timeout)//得到一个连接,//timeout是等待时间 public synchronized void release()//断开所有连接,释放占用的系统资源 private Connection newConnection()//新建一个数据库连接 public synchronized void TimerEvent() //定时器事件处理函数}public class DBConnectionManager { static private DBConnectionManager instance;//连接池管理类的唯一实例 static private int clients;//客户数量 private ArrayList drivers = new ArrayList();//容器,存放数据库驱动程序private HashMap pools = new HashMap ();//以name/value的形式存取连接池//对象的名字及连接池对象 static synchronized public DBConnectionManager getInstance()//如果唯一的//实例instance已经创建,直接返回这个实例;否则,调用私有构造函数,创//建连接池管理类的唯一实例 private DBConnectionManager()//私有构造函数,在其中调用初始化函数init()public void freeConnection(String name, Connection con)// 释放一个连接,//name是一个连接池对象的名字public Connection getConnection(String name)//从名字为name的连接池对象//中得到一个连接public Connection getConnection(String name, long time)//从名字为name//的连接池对象中取得一个连接,time是等待时间public synchronized void release()//释放所有资源private void createPools(Properties props)//根据属性文件提供的信息,创建//一个或多个连接池private void init()//初始化连接池管理类的唯一实例,由私有构造函数调用private void loadDrivers(Properties props)//装载数据库驱动程序} 如果用类似上面的数据库连接池类,需不需要先在服务器上配置数据库连接池
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MyServletJspOrEjb {
public void doSomething() throws Exception {
/*
* Create a JNDI Initial context to be able to
* lookup the DataSource
**
In production-level code, this should be cached as
* an instance or static variable, as it can
* be quite expensive to create a JNDI context.
**
Note: This code only works when you are using servlets
* or EJBs in a J2EE application server. If you are
* using connection pooling in standalone Java code, you
* will have to create/configure datasources using whatever
* mechanisms your particular connection pooling library
* provides.
*/
InitialContext ctx = new InitialContext();
/*
* Lookup the DataSource, which will be backed by a pool
* that the application server provides. DataSource instances
* are also a good candidate for caching as an instance
* variable, as JNDI lookups can be expensive as well.
*/
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
/*
* The following code is what would actually be in your
* Servlet, JSP or EJB 'service' method...where you need
* to work with a JDBC connection.
*/
Connection conn = null;
Statement stmt = null;
try {
conn = ds.getConnection();
/*
* Now, use normal JDBC programming to work with
* MySQL, making sure to close each resource when you're
* finished with it, which allows the connection pool
* resources to be recovered as quickly as possible
*/
stmt = conn.createStatement();
stmt.execute("SOME SQL QUERY");
stmt.close();
stmt = null;
conn.close();
conn = null;
} finally {
/*
* close any jdbc instances here that weren't
* explicitly closed during normal code path, so
* that we don't 'leak' resources...
*/
if (stmt != null) {
try {
stmt.close();
} catch (sqlexception sqlex) {
// ignore -- as we can't do anything about it here
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (sqlexception sqlex) {
// ignore -- as we can't do anything about it here
}
conn = null;
}
}
}
}
是不是通过类似上面的代码,不用配置服务器也能设置数据库连接池啊
private int checkedOut;//已被分配出去的连接数
private ArrayList freeConnections = new ArrayList();//容器,空闲池,根据//创建时间顺序存放已创建但尚未分配出去的连接
private int minConn;//连接池里连接的最小数量
private int maxConn;//连接池里允许存在的最大连接数
private String name;//为这个连接池取个名字,方便管理
private String password;//连接数据库时需要的密码
private String url;//所要创建连接的数据库的地址
private String user;//连接数据库时需要的用户名
public Timer timer;//定时器
public DBConnectionPool(String name, String URL, String user, String
password, int maxConn)//公开的构造函数
public synchronized void freeConnection(Connection con) //使用完毕之后,//把连接返还给空闲池
public synchronized Connection getConnection(long timeout)//得到一个连接,//timeout是等待时间
public synchronized void release()//断开所有连接,释放占用的系统资源
private Connection newConnection()//新建一个数据库连接
public synchronized void TimerEvent() //定时器事件处理函数}public class DBConnectionManager {
static private DBConnectionManager instance;//连接池管理类的唯一实例
static private int clients;//客户数量
private ArrayList drivers = new ArrayList();//容器,存放数据库驱动程序private HashMap pools = new HashMap ();//以name/value的形式存取连接池//对象的名字及连接池对象
static synchronized public DBConnectionManager getInstance()//如果唯一的//实例instance已经创建,直接返回这个实例;否则,调用私有构造函数,创//建连接池管理类的唯一实例 private DBConnectionManager()//私有构造函数,在其中调用初始化函数init()public void freeConnection(String name, Connection con)// 释放一个连接,//name是一个连接池对象的名字public Connection getConnection(String name)//从名字为name的连接池对象//中得到一个连接public Connection getConnection(String name, long time)//从名字为name//的连接池对象中取得一个连接,time是等待时间public synchronized void release()//释放所有资源private void createPools(Properties props)//根据属性文件提供的信息,创建//一个或多个连接池private void init()//初始化连接池管理类的唯一实例,由私有构造函数调用private void loadDrivers(Properties props)//装载数据库驱动程序} 如果用类似上面的数据库连接池类,需不需要先在服务器上配置数据库连接池
项目中需要采集外部数据源数据,这部分数据源的相关数据源配置是动态不定的,所以放在数据库中,可以供用户动态修改!而我通过数据连接池进行常规的数据处理,当需要处理这部分外部数据源数据时,只能采用jdbc进行处理,所以数据操作的性能多少受到一些影响,虽然影响不大,但还是想自己做一个数据连接池的操作类来实现!
最近在考虑是否直接能够访问处理Tomcat自身的数据连接池访问,希望近期能有所突破!