【发布时间】:2011-04-19 14:53:21
【问题描述】:
当尝试更新某个表时,它会失败,但会出现以下异常:“超过锁定等待超时;尝试重新启动事务”。 一些信息:我有两个表,profile 和 profile_units。 ID是profile表的主键,ID是profile_units中主键的一部分,也是profile中ID的外键。 当我调用 saveProfileChanges 时,updateAllFields 方法成功,但 addStmt.executeUpdate();在 handleActivityUnitsChanges 中失败并出现上述异常。 我使用 MySQL v5.0 作为数据库。我做错了什么?
我尝试执行以下代码:
public static Profile saveProfileChanges(Profile profile, List unitsToAdd)
throws Exception
{
Connection con = null;
try
{
con = ConnectionManager.getConnection();
updateAllFields(con, profile);
handleActivityUnitsChanges(con, profile, unitsToAdd);
con.commit();
return profile;
}
finally
{
ConnectionManager.closeConnection(con);
}
}
private static void handleActivityUnitsChanges(Connection con, Profile profile, List<ActivityUnit> unitsToAdd) throws Exception
{
PreparedStatement addStmt = null;
try
{
for (ActivityUnit currentUnitToAdd : unitsToAdd)
{
String sqlStatement = "insert into profile_units (ID, ActivityUnit) values (?, ?)";
addStmt = con.prepareStatement(sqlStatement);
addStmt.setLong(1, profile.getId());
addStmt.setLong(2, currentUnitToAdd.getId());
System.out.println(sqlStatement);
addStmt.executeUpdate();
addStmt.close();
}
}
catch (Exception e)
{
con.rollback();
throw e;
}
finally
{
ConnectionManager.closeStatement(addStmt);
}
}
public static Connection getConnection() throws Exception
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql:///someproject", "a", "a");
con.setAutoCommit(false);
return con;
}
【问题讨论】:
-
insert into profile_units )ID, ActivityUnit) values (?, ?)- 这是真实的代码吗? -
不,应该是 (ID, ActivityUnit) 值 (?, ?)
-
您的表是否被其他进程锁定?以sql工具为例?
-
如何运行这段代码?你还有其他并发交易吗?
-
我不认为有任何其他进程正在运行 - 为了确保我杀死了 MYSql 服务,重新启动它并再次运行它,结果相同(坏)。我从不同的类运行这段代码,并且只运行这段代码。我认为不存在任何其他并发事务。有没有办法检查? (我尝试在 MYSQL 中查看管理面板,但没有发现任何有用的信息)。