//DBConnection.java import java.sql.*; import java.io.IOException; import oracle.jdbc.pool.OraclePooledConnection;public class DBConnection { /* *oracle数据池连接 */ private static OraclePooledConnection pool = null; private static String url = "jdbc:oracle:thin:@192.168.0.1:1521:orcl"; private static String user = "admin"; private static String password = "admin"; private static Object providerLock = new Object(); public static Connection getConnection(){ Connection conn = null; if (pool == null) { synchronized (providerLock){ try { pool = new OraclePooledConnection(url,user,password); } catch (SQLException ex) { System.out.println("Error in Connecting to the Database "+'\n'+ex.toString()); } } } try { conn = pool.getConnection(); } catch (SQLException e) { System.err.println("Warring:DBConnection.getConnection() fail to get Connection source"); } if (conn == null) { System.err.println("Warring:DBConnection.getConnection() is failed"); } return conn; } }
获得连接就用 Connection con = DBConnection.getConnection();
不一定,说的太绝对. The DataSource interface is implemented by a driver vendor. There are three types of implementations: Basic implementation -- produces a standard Connection object Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager. Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.
to xiaofenguser(风雨) ( ) : 再请教一个问题:DBConnection的作用就是保证一个应用只创建一个OraclePooledConnection对象,能否用tomcat启动时创建OraclePooledConnection,这样就可以直接调用 OraclePooledConnection.getConnection(),而不用DBConnection类?
import java.sql.*;
import java.io.IOException;
import oracle.jdbc.pool.OraclePooledConnection;public class DBConnection
{
/*
*oracle数据池连接
*/
private static OraclePooledConnection pool = null;
private static String url = "jdbc:oracle:thin:@192.168.0.1:1521:orcl";
private static String user = "admin";
private static String password = "admin";
private static Object providerLock = new Object(); public static Connection getConnection(){
Connection conn = null;
if (pool == null)
{
synchronized (providerLock){
try
{
pool = new OraclePooledConnection(url,user,password);
}
catch (SQLException ex)
{
System.out.println("Error in Connecting to the Database "+'\n'+ex.toString());
}
}
}
try
{
conn = pool.getConnection();
}
catch (SQLException e)
{
System.err.println("Warring:DBConnection.getConnection() fail to get Connection source");
}
if (conn == null)
{
System.err.println("Warring:DBConnection.getConnection() is failed");
}
return conn;
}
}
Connection con = DBConnection.getConnection();
The DataSource interface is implemented by a driver vendor. There are three types of implementations: Basic implementation -- produces a standard Connection object
Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.
Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.
再请教一个问题:DBConnection的作用就是保证一个应用只创建一个OraclePooledConnection对象,能否用tomcat启动时创建OraclePooledConnection,这样就可以直接调用
OraclePooledConnection.getConnection(),而不用DBConnection类?
OraclePooledConnection.getConnection()应该不能用吧.它里面没有getConnection()这个静态方法.
你也可以参考 kofwr(搭补流二)的意见