81,094
社区成员
发帖
与我相关
我的任务
分享
<Resource name="jdbc/shjjhy"
auth="Container" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@***"
username="***"
password="***"
testOnBorrow="true"
testWhileIdle="true"
testOnReturn="true"
validationQuery="select 1 from dual"
minEvictableIdleTimeMillis="10000"
timeBetweenEvictionRunsMillis="10000"
maxActive="500"
maxIdle="50"
maxWait="1000" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" initialSize="5" />
<bean id="dataSourceJNDI" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/shjjhy</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
abstract="false" lazy-init="false">
<property name="dataSource">
<ref bean="dataSourceJNDI" />
</property>
</bean>
WebApplicationContext ctx = (WebApplicationContext) request
.getAttribute("WebApplicationContext");
JdbcTemplate jt = (JdbcTemplate) ctx.getBean("jdbcTemplate");
WebApplicationContext ctx = (WebApplicationContext) request.getAttribute("WebApplicationContext");
JdbcTemplate jt = (JdbcTemplate) ctx.getBean("jdbcTemplate");
Connection con = null;
try{
con = jt.getDataSource().getConnection();
con.setAutoCommit(false);
/**
* 业务操作
*/
con.setAutoCommit(true);
}catch(Exception e){
e.printStackTrace();
try {
con.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
DbUtil.closeConnection(con, true);
}
String emptyBillSql = "select CNTR_EF_ID from ship_bill where bill_no = ?";
try {
WebApplicationContext ctx = (WebApplicationContext) request
.getAttribute("WebApplicationContext");
JdbcTemplate jt = (JdbcTemplate) ctx.getBean("jdbcTemplate");
for (String bill : bills) {
if (!StringUtil.isEmpty(bill)) {
bi = bill.split("-");
String billEFId = "F";
try {
billEFId = jt.queryForObject(emptyBillSql, new Object[]{bi[0]}, String.class);
} catch (Exception e) {
e.printStackTrace();
billEFId = "F";
}
// 以下代码省略
有的地方使用的是jt.queryForList 也一样的报错;就是那种突然就出现了,然后后台疯狂报错,全都是关闭的连接这样的错误。所以我也是怀疑是不是因为单例模式的影响,在某个地方把JDBCTemplate的连接关闭了,其他地方只要引用JDBCTemplate就都报错了。
但是我也不知道是什么样的代码能做到把JDBCTemplate的连接关闭而且让Spring不会新获取连接。
WebApplicationContext ctx = (WebApplicationContext) request.getAttribute("WebApplicationContext");
JdbcTemplate jt = (JdbcTemplate) ctx.getBean("jdbcTemplate");
Connection con = null;
try{
con = jt.getDataSource().getConnection();
con.setAutoCommit(false);
/**
* 业务操作
*/
con.setAutoCommit(true);
}catch(Exception e){
e.printStackTrace();
try {
con.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
DbUtil.closeConnection(con, true);
}
然后上面的DbUtil.closeConnection(con, true);的方法如下:
public static boolean closeConnection(Connection connection,
boolean isExcept) {
/* 331 */boolean status = true;
try {
/* 333 */if ((connection != null) && (!connection.isClosed())) {
/* 334 */if (!connection.getAutoCommit())
try {
/* 336 */if (isExcept)
/* 337 */connection.rollback();
else
/* 339 */connection.commit();
} catch (Exception e) {
/* 341 */status = false;
/* 342 */if (!isExcept)
/* 343 */connection.rollback();
} finally {
/* 345 */connection.setAutoCommit(true);
}
/* 347 */connection.close();
/* 348 */connection = null;
}
} catch (Exception e) {
/* 351 */status = false;
}
/* 353 */return status;
}
再次感谢大神的指导!