站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题急!tomcat连接池的问题(apache+tomcat)

急!tomcat连接池的问题(apache+tomcat)

摘要: 急!tomcat连接池的问题(apache+tomcat)


在单个tomcat的环境下没有任何问题,我的配置文件如下:
D:/tomcat55/conf/Catalina/localhost/MyWeb.xml
=========
antiResourceLocking="false" antiJARLocking="false" reloadable="true" >
auth="Container"
description="DB Connection"
driverClass="oracle.jdbc.driver.OracleDriver"

maxPoolSize="50"
minPoolSize="5"
acquireIncrement="1"
name="jdbc/oracle9"
user="test"
password="test"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@192.168.1.2:1521:ora9i" />


应用全部放在了 D:/tomcat55/webapps/MyWeb

但现在放到正式的服务器上后(环境是 apache+tomcat5.5.17)是通过虚拟主机方式访问的
在tomcat55/conf/server.xml里添加了一个Host,如下:


原来的路径也变为: D:/tomcat55/webapps/MyWeb/ROOT
(多加了个ROOT,原来在MyWeb下的所有jsp等均被移到ROOT下了)

请问这时我该怎么配置连接池?




server.xml中:


MyWeb.xml 中只要


auth="Container"
description="DB Connection"
driverClass="oracle.jdbc.driver.OracleDriver"

maxPoolSize="50"
minPoolSize="5"
acquireIncrement="1"
name="jdbc/oracle9"
user="test"
password="test"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@192.168.1.2:1521:ora9i" />






不行啊!!


你可以考虑不使用数据源的方法来获得连接?
package database;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

public class ConnectionPool {

private final String DBDriver = "com.mysql.jdbc.Driver";
private final String schema = "examsystem";
private final String user = "root";
private final String password = "comeon";
private final static String poolName = "connectionpool";

private final String driverInfo
= "jdbc:mysql://localhost/" + schema + "?user=" + user + "&password=" + password
+ "&useUnicode=true&characterEncoding=gb2312";

private static ConnectionPool connectionPool;

private ConnectionPool(){
System.out.println("Loading underlying JDBC driver.");
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");

System.out.println("Setting up driver.");
try {
setupDriver( driverInfo );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done.");
}

public static ConnectionPool getInstance () {

if ( connectionPool == null) {
connectionPool = new ConnectionPool();
}

return connectionPool;
}

public Connection getConnection() {
System.out.println("Creating connection.");
try {
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:connectionpool");
System.out.println("得到一个新的连接:/r/n" + conn.toString());
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

public static void setupDriver(String connectURI) throws Exception {
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//GenericObjectPool(org.apache.commons.pool.PoolableObjectFactory factory,
// int maxActive,
// byte whenExhaustedAction,
// long maxWait,
// int maxIdle,
// int minIdle,
// boolean testOnBorrow,
// boolean testOnReturn,
// long timeBetweenEvictionRunsMillis,
// int numTestsPerEvictionRun,
// long minEvictableIdleTimeMillis,
// boolean testWhileIdle)
ObjectPool connectionPool = new GenericObjectPool(null,
100,
GenericObjectPool.WHEN_EXHAUSTED_GROW,
10000,
50);

//
// Next, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);

//
// Now we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

//
// Finally, we create the PoolingDriver itself...
//
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

//
// ...and register our pool with it.
//
driver.registerPool(poolName,connectionPool);

//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
//
}

public static void printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
ObjectPool connectionPool = driver.getConnectionPool(poolName);

System.out.println("NumActive: " + connectionPool.getNumActive());
System.out.println("NumIdle: " + connectionPool.getNumIdle());
}

public static void shutdownDriver() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool(poolName);
}

}

使用getConnection()方法就可以从数据库连接池中获取连接了!


帮顶


MyWeb.xml



type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@jdbc:oracle:thin:@192.168.1.2:1521:ora9i"
username="test" password="test" maxActive="20" maxIdle="10"
maxWait="-1"/>





↑返回目录
前一篇: 请问:一般的网上支付是如何实现的?
后一篇: 网页浏览权限问题