错误的多次获取oracle.jdbc.driver.T4CConnection连接,导致tomcat突然停止

7 篇文章 0 订阅
/**
	 * 功能描述:获取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);
		}

所以啊,写代码一定要仔细啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值