Connection对象一直没有被释放

本文介绍了一个Java程序中处理数据库事务及调用存储过程的方法。该方法首先更新企业信息,接着插入员工记录,并调用存储过程创建相关表。但执行后发现表锁未正确释放,疑似与事务提交有关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下这个方法的目的是
1.先更新表sys_enterprise 里的一条记录
2.在到sys_staffs插入一条记录
3.然后 执行存储过程建里俩个表

问题是 为什么我执行成功后?到sql 2000 的查询分析器里运行

select * from sys_staffs 觉得表里的锁一直没释放呢?
难道每个 con 都要写一个 con.commit()函数
public boolean checkEnterprise(String id)
{
boolean flag = false;
EnterpriseEntity enterprise = this.getEnterpriseById(id);
String sql = "update sys_enterprise set e_isallowed = 1 where e_id = ?";
Connection con = dbc.getConnection();

CallableStatement cst = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {

con.setAutoCommit(false);
pst = con.prepareStatement(sql);
pst.setString(1, id);
pst.executeUpdate();

con = null;
con = dbc.getConnection();
con.setAutoCommit(false);
sql = "insert into sys_staffs(s_name,s_pwd,s_enterprisename) values(?,?,?)";
pst = con.prepareStatement(sql);
pst.setString(1, enterprise.getEname());
pst.setString(2, Secret.getMD5(enterprise.getEname()));
pst.setString(3, enterprise.getEname());
pst.executeUpdate();

con = null;
con = dbc.getConnection();
con.setAutoCommit(false);
sql = "{call proc_create_enterprise '"+enterprise.getEname()+"'}";
cst = con.prepareCall(sql);
cst.execute();

con.commit();
flag = true;
} catch (Exception e) {
flag = false;
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
dbc.close(rs, pst, con);
}
return flag;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值