Why I can't update the table when the transaction isolation level is serializable?
Posted by: coco huang
Date: April 11, 2005 11:33PM

My mysql's version is 4.0.18 and it's default isolation level is repeatable read .My code is below:

public class TestMysqlTransaction
{
private static void displayProducts(Statement myStatement) throws SQLException
{
ResultSet productResultSet = myStatement.executeQuery("select id,name from test");
while (productResultSet.next())
{
System.out.println(productResultSet.getInt("id") + " " + productResultSet.getString("name"));
}
productResultSet.close();
}

public static void main(String args[]) throws Exception
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection rcConnection = DriverManager.getConnection("jdbc:mysql://localhost/testtransaction","root","coco");
rcConnection.setAutoCommit(false);

Connection seConnection = DriverManager.getConnection("jdbc:mysql://localhost/testtransaction","root","coco");
seConnection.setAutoCommit(false);

seConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

Statement rcStatement = rcConnection.createStatement();
Statement seStatement = seConnection.createStatement();

displayProducts(rcStatement);
displayProducts(seStatement);
System.out.println("update .....");
rcStatement.executeUpdate("update test set name='new' where id= 0");
rcStatement.executeUpdate("insert into test values(1,'coco')");
rcConnection.commit();

System.out.println("update end");
displayProducts(rcStatement);
displayProducts(seStatement);
rcStatement.close();
rcConnection.close();
seStatement.close();
seConnection.close();
}


But I got the exception:

java.sql.SQLException: General error: Lock wait timeout exceeded; Try restarting transaction
at org.gjt.mm.mysql.MysqlIO.sendCommand(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.sqlQueryDirect(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.sqlQuery(Unknown Source)
at org.gjt.mm.mysql.Connection.execSQL(Unknown Source)
at org.gjt.mm.mysql.Connection.execSQL(Unknown Source)
at org.gjt.mm.mysql.Statement.executeUpdate(Unknown Source)
at org.gjt.mm.mysql.jdbc2.Statement.executeUpdate(Unknown Source)
at TestMysqlTransaction.main(TestMysqlTransaction.java:46)

Who can help me?

Options: ReplyQuote


Subject
Written By
Posted
Why I can't update the table when the transaction isolation level is serializable?
April 11, 2005 11:33PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.