这个JAVA事务问题在哪?为什么有时候只减了汇款者金额却没给收款者加?

pressman 2008-10-13 10:32:37
	public boolean agentUpdateCustomerBalance(int agent_id,String  username,int count)//代理给客户

{
DBHelper helper=new DBHelper();
Connection conn=helper.getConn();

int res=0;
try
{
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);


PreparedStatement ps=helper.createPreparedStatement("select balance from boss_agent where id=?");
ps.setInt(1,agent_id);
ResultSet rs=ps.executeQuery();
if(rs.next()&& rs.getInt("balance")-count>=0)
{
ps=helper.createPreparedStatement("select money_remain from users where username=?");
ps.setString(1, username);
rs=ps.executeQuery();
if(rs.next()&& rs.getInt("money_remain")+count>=0)
{
// conn.setAutoCommit(false);
// conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);


ps=helper.createPreparedStatement("update boss_agent set balance=ifnull(balance,0)-(?) where id=?");
ps.setInt(1, count);
ps.setInt(2, agent_id);
res+=ps.executeUpdate();
System.out.println("Jian"+ps.toString());

ps=helper.createPreparedStatement("update users set money_remain=ifnull(money_remain,0)+(?) where username=?");
ps.setInt(1, count);
ps.setString(2, username);
res+=ps.executeUpdate();
System.out.println("Jia"+ps.toString());

if(res==2){
System.out.println("conn.commit();2");
conn.commit();

}
else{
System.out.println("conn.rollback();2");
conn.rollback();
}

ps=helper.createPreparedStatement("select user_id from users where username=?");
ps.setString(1, username);
rs=ps.executeQuery();
int user_id=0;
if(rs.next())
user_id=rs.getInt("user_id");
ps=helper.createPreparedStatement("update boss_customer set total_balance=ifnull(total_balance,0)+(?) where user_id=?");
ps.setInt(1, count);
ps.setInt(2, user_id);
res+=ps.executeUpdate();
if(res==3)
conn.commit();
else
conn.rollback();
}



}
conn.commit();

} catch (SQLException e)
{
try
{
conn.rollback();
} catch (SQLException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
helper.closeAll();
return res==3;
}
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pressman 2008-10-13
  • 打赏
  • 举报
回复
大致清楚了,谢谢以上各位的帮助,现在结贴
Landor2004 2008-10-13
  • 打赏
  • 举报
回复
update boss_agent 和update boss_customer 中间显然有个if xxx conn.commit();
提交之后,代码继续执行,如果出现异常,那么先前的事务已经提交!

就是这里出的问题
hy0231 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pressman 的回复:]
引用 2 楼 hy0231 的回复:
在异常处理时应该加上conn.setAutoCommit(true);


异常了,还启动自动提交?
症结是在这里吗??
不理解,能说得详细点吗?谢谢你。
[/Quote]
是的.你的代码写的也有问题.
if(res==2){
System.out.println("conn.commit();2");
conn.commit();

}
这里做了提交.在最后你又做了提交.所以这个事务更本就没起到该有的作用.(有好几处这样的)
同时建议在异常是加上conn.setAutoCommit(true); 因为如果你在异常时,没有执行commit,所以你的连接还处于setAutoCommit(false)状态.
pressman 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hy0231 的回复:]
在异常处理时应该加上conn.setAutoCommit(true);
[/Quote]

异常了,还启动自动提交?
症结是在这里吗??
不理解,能说得详细点吗?谢谢你。
xuhua205 2008-10-13
  • 打赏
  • 举报
回复
建议使用spring的声明式事务机制,配置简单,不需要写Java代码
hy0231 2008-10-13
  • 打赏
  • 举报
回复
在异常处理时应该加上conn.setAutoCommit(true);
pressman 2008-10-13
  • 打赏
  • 举报
回复
我看来看去没看出问题来,但实际应用中出现:
update boss_agent set balance=ifnull(balance,0)-(?) where id=? 执行成功了,但
update boss_customer set total_balance=ifnull(total_balance,0)+(?) where user_id=? 却没有被执行或者执行失败。

导致整个过程失去了原子性,数据操作安全出现重大问题!
请问各位高手,问题在哪?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧