Java SQL Server Application Stuck On statement.executeUpdate



我有一个相当烦人的问题。在下面的代码段中,我尝试在数据库中的"RevisionDispersion"表中插入一个新行。但是,每当我调用stmt.executeUpdate((时,程序都会冻结,最终没有数据库的事务。无论我等多久;数据库只是不会更新。以下是感兴趣的代码:

private static final String INSERT_DISPERSION = "insert into RevisionDispersion(" 
+ Assignments.ID + ", " 
+ Persons.EMAIL + ", " 
+ Handins.ID + ")" 
+ " values(?, ?, ?)";
public static void disperse(DataSource source, Assignment assignment) throws Exception
{
List<String> handins = assignment.getHandins();
//used to decide who checks which assignment
int maxRNG = Math.max(1, handins.size() / assignment.getPeerCount());
int rng = new Random().nextInt(maxRNG);
PreparedStatement stmt = null;
Connection con = null;
try{
//Get the connection, set it to TRANSACTION_SERIALIZABLE and set autocommit to false
con = source.getConnection();
configureConnection(con);
//Prepare the statement to insert the new dispersion
stmt = con.prepareStatement(INSERT_DISPERSION);
stmt.setString(1, assignment.getID());
//Iterate over all hand-ins and decide from which peer a peer receives feedback
for(int i = 0; i < handins.size(); i++)
{
HandIn handin = new HandIn(source.getConnection(), handins.get(i));
String student = handin.getEmail();
stmt.setString(2, student);
for(int j = 1; j <= assignment.getPeerCount(); j++)
{
HandIn otherHandin =  new HandIn(source.getConnection(), handins.get(j * rng));
stmt.setString(3, otherHandin.getId());
stmt.executeUpdate();
}
}
con.commit();
}catch(Exception e){
throw e;
}finally{
closeQuietly(con, stmt);
}
}
//This method is originally in the DBAO class, but I put it here for you folks.
protected static void configureConnection(Connection connection) throws SQLException
{
connection.setAutoCommit(false);
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
}

在应用程序中没有其他位置会出现此问题。每当我在SQL Server Management Studio中使用相同的参数运行SQL语句时,它都不会卡住,并且可以很好地插入新行。删除行并在应用程序中尝试相同的行后,它就会卡住。

谁能指出我出错的正确方向?我已经连续尝试了 3 个小时了...

我已经尝试过的东西

-使用 stmt.addBatch(( 而不是 executeUpdate(((没有区别。它会卡在 executeBatch(((

-检查所有连接是否正确关闭;它们是。

-检查其他使用修订分散表的语句/结果集是否仍然打开(没有一个仍然打开。即使有,我认为不应该有所作为吗?

- 完全删除数据库并设置备份

我解决了这个问题...

在另一段代码中,我有以下内容:

private static final String GET_NOT_DISPERSED = "select * from Assignments where "
+ Assignments.CLOSE_DATE + "<=? and " 
+ Assignments.PEER_START_DATE + ">=? and "
+ Assignments.ID + " not in(select " + Assignments.ID + " from RevisionDispersion)";
private void makeMailDispersion() throws Exception
{
DateTime currentDate = DateTime.getCurrentDateTime();
PreparedStatement assignmentsStmt = null;
ResultSet assignments = null;
Connection con = null;
try{
con = source.getConnection();
configureConnection(con);
assignmentsStmt = con.prepareStatement(GET_NOT_DISPERSED);
assignmentsStmt.setString(1, currentDate.toString());
assignmentsStmt.setString(2, currentDate.toString());
assignments = assignmentsStmt.executeQuery();
ArrayList<Assignment> requiresDispersion = new ArrayList<>();
assignments.close();
assignmentsStmt.close();
while(assignments.next())
{
Assignment assignment = new Assignment(source.getConnection(), assignments.getString(Assignments.ID));
AssignmentDisperser.disperse(source, assignment);
}
}catch(Exception e){
throw e;
}finally{
closeQuietly(con, assignmentsStmt, assignments);
}
}

在这段代码中,我关闭了变量"赋值"和"赋值Stmt"。我认为这足以在使用GET_NOT_DISPERSED查询后解锁表。显然不是:桌子仍然被锁着。

为了修复它,我必须做什么:除了调用assignments.close((和assignmentsStmt.close((之外,我还必须调用con.close((。这完全解锁了表并允许代码正常运行。

我也遇到了同样的问题,并通过在执行java代码之前关闭Oracle SQL开发人员来解决此问题。该软件被锁定在 executeUpdate(( 语句中。

最新更新