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 / General / March 2008

Tip: Looking for answers? Try searching our database.

is a MySQL write lock automatically released when a pooled connection     is closed?

Thread view: 
christopher@dailycrossword.com - 07 Mar 2008 21:29 GMT
Greetings,
I am using JDBC (probably version 2), and I use code like this to open
and close connections:

Connection con=null;
Statement sqlstatement=null;
ResultSet sqlresults=null;
PreparedStatement pStatement=null;
 try {
   con=DriverManager.getConnection("name of connection pool");
   ....
   } catch (SQLException e) {/*handle error*/);
   } finally {
   if(con!=null) try { con.close();} catch (SQLException e){}
   if(sqlstatement!=null) try { sqlstatement.close();} catch
(SQLException e){}
   if(pStatement!=null) try { pStatement.close();} catch
(SQLException e){}
   if(sqlresults!=null) try { sqlresults.close();} catch
(SQLException e){}
}

Which I think is like belt and suspenders.  In a new connection I will
be locking the table, and I need to be certain it is unlocked when I
am done.  I am planning to add the "unlock tables" update to the first
line of the finally block, before the close().  I am wondering what
will happen if there is a hiccup (broken pipe in the connection pooler
or something) -- does the lock ever get released?

   } catch (SQLException e) {/*handle error*/);
   } finally {
   if(con!=null) try {
    sqlstatement.execute("UNLOCK TABLES;");  // (or whatever
     con.close();
     } catch (SQLException e){}
   if(sqlstatement!=null) try { sqlstatement.close();} catch
(SQLException e){}
   if(pStatement!=null) try { pStatement.close();} catch
(SQLException e){}
   if(sqlresults!=null) try { sqlresults.close();} catch
(SQLException e){}
}

This really exposes a fundamental lack of understanding on my part
about the nature of connection pooling -- do temporary tables exist
for the life of pooled connections or just for the current use of the
connector?  Variables?  When are pooled connections ever actually
closed?

Thanx all!
christopher@dailycrossword.com - 10 Mar 2008 00:02 GMT
On Mar 7, 2:29 pm, christop...@dailycrossword.com wrote:
> Greetings,
> I am using JDBC (probably version 2), and I use code like this to open
> and close connections:

snip

I have examined the source for the pooled connection's close() method,
and it simply returns the connection object to a generic pool (Jakarta
commons DBCP and Pool), so it seems as though no reset is ever done to
release locks or close temporary tables, etc., as is is in other
pooled connectors.  I have read a number of posts from several years
ago either asking the same question, or declaring that 'table locks'
should never be used in 'robust applications'.  I am very concerned
that an uncaught exception or transient network failure would lock the
table indefinitely.

For what it's worth I am re-designing that portion of the application
to produce meaningful (if a little stale) data without using the table
locks.

Cheers!
Arne Vajhøj - 10 Mar 2008 02:51 GMT
> On Mar 7, 2:29 pm, christop...@dailycrossword.com wrote:
>> Greetings,
>> I am using JDBC (probably version 2), and I use code like this to open
>> and close connections:

> I have examined the source for the pooled connection's close() method,
> and it simply returns the connection object to a generic pool (Jakarta
[quoted text clipped - 9 lines]
> to produce meaningful (if a little stale) data without using the table
> locks.

LOCK TABLE is a hack. A hack that does not fit well with Java.

InnoDB tables, transactions and a suitable transaction isolation
level.

Arne


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.