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

pressman pressman 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
	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;
}
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
pressman
Web 开发 81,108 成员 341,721 内容