一、Tomcat6.0中配置数据源
1.在Tomcat根目录/conf/Catalina/localhost目录下新建一个XML文件,文件名称跟工程名称一致.文件中的内容如下:
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="Oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.2.250:1521:hmisb"
username="mtms" password="mtms"
validationQuery="select 1 from dual"
maxIdle="100" maxActive="500" maxWait="1000" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true"/>
2.在tomcat的conf/context.xml中的标签中添加一个,内容如下:
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="500" maxWait="10000"
username="oa" password="oa" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@172.16.60.30:1521:HMIS" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true" />
然后在web.xml中添加引用(tomcat5.5以上可以不用添加这一段)
DB Connection
jdbc/oa
javax.sql.DataSource
Container
2.1.获取连接对象
public class ConnectionPool{
public static Connection getConn()throws Exception{
//初始化上下文
Context initContext=getInitContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource dataSource=(DataSource)envContext.lookup("jdbc/oa");
//获取连接对象
return ds.getConnection();
}
}
docBase是指Web工程所在的位置,path是工程的名称, name是指JNDI的名称,type是数据源的类型,driverClassName是驱动名称,url是驱动的连接字符串
username是指数据库的用户名,password是指数据库的密码,defaultAutoCommit是否自动提交
二、Tomcat5.x中配置全局连接池
1.打Tomcat根目录/conf/server.xml文件,在标签下添加
factory
org.apache.commons.dbcp.BasicDataSourceFactory
driverClassName
oracle.jdbc.driver.OracleDriver
url
jdbc:oracle:thin:@172.16.61.13:1521:服务名
username
clgc
password
clgc
validationQuery
select count(*) from dual
maxIdle
5
maxActive
10
maxWait
-1
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
true
2.在Host标签下添加标签
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
这里的
path是指项目发布在tomcat webapps目录下的路径,我的项目目录为:../tomcat5.0/webapps/rldmpda
docBase是指项目的
在JSP里有两种实现的办法,一种是用JNDI(Java Naming Directory Interface),这可能和应用服务器有关,如果是Resin,先在resin.conf里定义
jdbc/oracle
javax.sql.DataSource
如果为Tomcat,在Server.xml里面定义,有关的资料可以查文档,然后在jsp里这样用
try{
javax.naming.Context env = (Context)new InitialContext().lookup("java:comp/env");
javax.sql.DataSource pool=(javax.sql.DataSource) env.lookup("jdbc/oracle");
}catch(Exception e){System.err.println("Exception error:"+e.getMessage());}
try {
Connection conn = pool.getConnection();
}catch(Exception e){System.out.println("Exception error:"+e.getMessage());}
通过这段代码,你就获得从连接池里获得了一个连接conn。如果想用普通的连接池,那只能用JavaBean了,先写一个ConnectionPool的java的类,然后直接从连接池中获得连接,下面是我一个连接池的JavaBean
ConnectionPool.java如下:
import java.io.PrintStream;
import java.sql.Connection;
import java.util.Vector;
// Referenced classes of package com.ilovejsp.sql:
// DataSource, PooledConnection
public class ConnectionPool
{
private Vector pool;
private int size;
DataSource db;
public ConnectionPool()
{
pool = null;
size = 0;
db = new DataSource();
}
public void setSize(int value)
{
if(value > 1)
size = value;
}
public int getSize()
{
return size;
}
public synchronized void initPool()
throws Exception
{
try
{
for(int x = 0; x < size; x++)
{
Connection conn = db.getConnection();
if(conn != null)
{
PooledConnection pcon = new PooledConnection(conn);
addConnection(pcon);
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
private void addConnection(PooledConnection pcon)
{
if(pool == null)
pool = new Vector(size);
pool.addElement(pcon);
}
public synchronized void releaseConnection(Connection conn)
{
int x = 0;
do
{
if(x >= pool.size())
break;
PooledConnection pcon = (PooledConnection)pool.elementAt(x);
if(pcon.getConnection() == conn)
{
System.err.println("Release Connection".concat(String.valueOf(String.valueOf(x))));
pcon.setInUse(false);
break;
}
x++;
}
while(true);
}
public synchronized Connection getConnection()
throws Exception
{
PooledConnection pcon = null;
for(int x = 0; x < pool.size(); x++)
{
pcon = (PooledConnection)pool.elementAt(x);
if(!pcon.inUse())
{
pcon.setInUse(true);
return pcon.getConnection();
}
}
try
{
Connection conn = db.getConnection();
pcon = new PooledConnection(conn);
pcon.setInUse(true);
pool.addElement(pcon);
}
catch(Exception e)
{
System.err.println("Exception error:".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for(int x = 0; x < pool.size(); x++)
{
System.err.println("Closing Jdbc Connection".concat(String.valueOf(String.valueOf(x))));
PooledConnection pcon = (PooledConnection)pool.elementAt(x);
if(!pcon.inUse())
{
pcon.close();
continue;
}
try
{
Thread.sleep(3000L);
pcon.close();
}
catch(Exception e)
{
System.out.println("Exception :".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
}
db.close();
}
}
testpool.jsp内容如下:
系统数据信息Connection conn=db.getConnection();
Statement stmt=conn.createStatement();
String sql1="select * from pg_database ";
ResultSet rs=stmt.executeQuery(sql1);
%>
系统数据库信息 |
%> rs.close();%> |
系统字段信息 |
rs=stmt.executeQuery(sql2); while(rs.next()) { %> () rs.close(); db.close();%> |