/**
* 功能描述:获取oracle连接
* @param con
* @return
* @throws SQLException
*/
public static Connection getOracleConnection() throws SQLException {
Connection conn = getConnection();// 获取connection
if (conn instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) conn).getInnermostDelegate();
/* For some reason, the innermost delegate can be null: not for a
* Statement's Connection but for the Connection handle returned by the pool.
* We'll fall back to the MetaData's Connection in this case, which is
* a native unwrapped Connection with Commons DBCP 1.1.
*/
return (nativeCon != null ? nativeCon : conn.getMetaData().getConnection());
}
return conn;
}
获取完这个连接后,又有了一下的代码
try {
conn = DataAccess.getOracleConnection();
String sql = "{call updateStorageFlag(?,?,?,?)}";
proc = conn.prepareCall(sql);
ArrayDescriptor desc = ArrayDescriptor.createDescriptor("ARRAY_BARCODEID", conn);// 大写,不然会报“无效的名称模式”
ARRAY arr1 = new ARRAY(desc, conn, barcodeArray);
desc = ArrayDescriptor.createDescriptor("ARRAY_BOXID", conn);// 大写,不然会报“无效的名称模式”
ARRAY arr2 = new ARRAY(desc, conn, boxArray);
proc.setString(1, allocationListNo);// 订单号
proc.setObject(2, arr1, Types.ARRAY);// 条形码数组
proc.setObject(3, arr2, Types.ARRAY);// 箱号数组
proc.setString(4, flag);
proc.execute();
// System.out.println("成功的记录数="+resultList.get(0));
// System.out.println("未成功插入的条形码="+resultList.get(1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
sign = false;
}finally{
DataAccess.Close(conn, proc);
}
System.out.println("是否关闭-"+conn.isClose()+" conn="+conn);<span style="color:#FF0000;">// 虽然结果是true,但是conn还是有值的(我的连接池用的是dbcp)</span>
当我把DBCP的最大活动连接设为10时,当点击第11次时,tomcat就直接down掉了,百思不得其解啊,最后仔细琢磨琢磨原来是连接池中的连接根本就没关掉,连接池已经耗尽了,将上面的方式改改,最终的代码如下:
/**
* 功能描述:获取oracle连接
* @param con
* @return
* @throws SQLException
*/
public static Connection getOracleConnection(<span style="color:#FF0000;">Connection conn</span>) throws SQLException {
if (conn instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) conn).getInnermostDelegate();
/* For some reason, the innermost delegate can be null: not for a
* Statement's Connection but for the Connection handle returned by the pool.
* We'll fall back to the MetaData's Connection in this case, which is
* a native unwrapped Connection with Commons DBCP 1.1.
*/
System.out.println("getOracleConnection="+(nativeCon != null ? nativeCon : conn.getMetaData().getConnection()));
return (nativeCon != null ? nativeCon : conn.getMetaData().getConnection());
}
return conn;
}
try {
conn = DataAccess.getConnection();
String sql = "{call updateStorageFlag(?,?,?,?)}";
proc = conn.prepareCall(sql);
ArrayDescriptor desc = ArrayDescriptor.createDescriptor("ARRAY_BARCODEID", DataAccess.getOracleConnection(conn));// 大写,不然会报“无效的名称模式”
ARRAY arr1 = new ARRAY(desc, DataAccess.getOracleConnection(conn), barcodeArray);
desc = ArrayDescriptor.createDescriptor("ARRAY_BOXID", DataAccess.getOracleConnection(conn));// 大写,不然会报“无效的名称模式”
ARRAY arr2 = new ARRAY(desc, DataAccess.getOracleConnection(conn), boxArray);
proc.setString(1, allocationListNo);// 订单号
proc.setObject(2, arr1, Types.ARRAY);// 条形码数组
proc.setObject(3, arr2, Types.ARRAY);// 箱号数组
proc.setString(4, flag);
proc.execute();
// System.out.println("成功的记录数="+resultList.get(0));
// System.out.println("未成功插入的条形码="+resultList.get(1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
sign = false;
}finally{
DataAccess.Close(conn, proc);
}
所以啊,写代码一定要仔细啊