Hi,
I'm using MySQL database with Java and the connector (in subject line).
I have read and searched the group prior to posting, and find no clear
answer.
I would like to lock my table for writing, so would it be like this?
I have heard the suggestion, that executeUpdate in MySQL does a table
lock automatically, is this true?
Please feel free to instruct/assist.... Thanks..
try{
Statement stmt = connection.createStatement();
stmt.execute("LOCK TABLES databaseName.tableName WRITE");
int rowNumber = stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.out.println("SQL Exception" + ex.getMessage() );
throw new SQLException(ex.getMessage() ,ex.getSQLState() );
}
finally{
Statement stmt = connection.createStatement();
stmt.executeUpdate("UNLOCK TABLES");
stmt.close();
}
Northernboy - 28 Sep 2005 19:47 GMT
Ok, I'll answer my own question, in case anyone else is interested.
two fixes to above will allow it work fine. The close statements are
put into the finally block, as this gets executed whether or not there
is an exception.
enjoy............
Statement stmt = null;
try{
stmt = connection.createStatement();
stmt.execute("LOCK TABLES mytablename WRITE");
int rowNumber = stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.out.println("SQL Exception caught + ex.getMessage() );
throw new SQLException(ex.getMessage() ,ex.getSQLState() );
}
finally{
if(stmt != null){
stmt.execute("UNLOCK TABLES");
stmt.close();
}
}