Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / September 2005

Tip: Looking for answers? Try searching our database.

setting lock table on mysql via java mysql-connector-java-3.1.7-bin.jar

Thread view: 
Northernboy - 27 Sep 2005 17:54 GMT
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();
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.