1.下载 :在 Torque 的使用过程中,很多的工作都是通过 ant 来完成的,所以先下载并安装 ant。然后在 http://db.apache.org/torque/ 网站上下载 torque。
2.把 %Torque_home%/lib下面的所有.jar文件,放入测试项目classpath中
3.Torque.properties设置
torque.applicationRoot = .
log4j.category.org.apache.torque = ALL, org.apache.torque
log4j.appender.org.apache.torque = org.apache.log4j.ConsoleAppender
log4j.appender.org.apache.torque.file = ${torque.applicationRoot}/logs/torque.log
log4j.appender.org.apache.torque.layout = org.apache.log4j.PatternLayout
log4j.appender.org.apache.torque.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.org.apache.torque.append = false
torque.database.default=test
torque.database.mdclub.adapter=oracle
torque.database.vcmsdb.adapter=oracle
## Using Jdbc2Pool源
torque.dsfactory.test.factory=org.apache.torque.dsfactory.Jdbc2PoolDataSourceFactory
torque.dsfactory.test.pool.defaultMaxActive=10
torque.dsfactory.test.pool.testOnBorrow=true
torque.dsfactory.test.pool.validationQuery=SELECT * from dual
torque.dsfactory.test.connection.driver = oracle.jdbc.driver.OracleDriver
torque.dsfactory.test.connection.url = jdbc:oracle:thin:@192.168.0.110:1521:ora10
torque.dsfactory.test.connection.user = test
torque.dsfactory.test.connection.password = test
torque.idbroker.cleverquantity=true
torque.manager.useCache = true
4.Torque的使用(只使用其连接池)
java.sql.Connection conn = Torque.getConnection();
String sql =
"select pid from person where phone_number = '"+phone_number+"'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
pid = (int)rs.getInt("pid");
break;
}
if (conn != null) {
Torque.closeConnection(conn);
}
5. 测试500次数据库连接,未用Torque连接池:所需时间:30s
用Torque连接池:9s
6. 总结:也可以用hibernate的连接池,自己写连接池编写,调试可能出错,配置tomcat连接池则可能与服务器变动而不能使用,使用Torque连接池简单,与应用服务器无关