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 / October 2003

Tip: Looking for answers? Try searching our database.

Multi-threading and PreparedStatements - Also rolling back table creation

Thread view: 
Ellarco - 13 Oct 2003 00:22 GMT
Please consider the sample class below (ignoring any irrelevant mistakes...
Im a novice).

class A {
 protected static final String psString = "INSERT INTO some_table
VALUES(?)";
 protected static java.sql.PreparedStatement ps = null;

 protected final String s;

 /** I know this bit is very ugly and am open to suggestions? **/
 public static init(java.sql.Connection con) throws java.sql.SQLException {
   if(A.ps == null)
     A.ps = con.prepareStatement(A.psString);
 }

 public A(String s) {
   this.s = s;
 }

 public java.sql.Statement getStatement() throws java.sql.SQLException {
   A.ps.setString(1, this.s);
   return A.ps;
 }
}

If I were to;

1. Create database connection con
2. Use con to init the A class
3. Create multiple threads which instantiate A and invoke getStatement() on
those instances, placing the resulting java.sql.Statements into a batch (we
synchronise the addBatch()s)
4. Execute the batch (from the main thread)

I am worrying that if getStatement() is invoked concurrently the
"A.ps.setString(1, this.s);" might interfere with each other. Do I need to
worry?

Also, an unrelated question - is it possible to roll back CREATE TABLE
statements? I havent been able to.

Thanks.
Lee Fesperman - 16 Oct 2003 04:26 GMT
> Please consider the sample class below (ignoring any irrelevant mistakes...
> Im a novice).
[quoted text clipped - 34 lines]
> "A.ps.setString(1, this.s);" might interfere with each other. Do I need to
> worry?

Yes, setString() changes the internal state of the PreparedStatement which the
subsequent addBatch() then uses. There is no way for the JDBC driver to be thread-safe
here.

You need to synchronize any setXXX() and the addBatch(). You also need to make sure all
threads are finished before the executeBatch().

> Also, an unrelated question - is it possible to roll back CREATE TABLE
> statements? I havent been able to.

That depends on the DBMS. dataDefinitionIgnoredInTransactions() in DatabaseMetaData
might tell you the situation.

Signature

Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Ellarco - 17 Oct 2003 01:15 GMT
> > Please consider the sample class below (ignoring any irrelevant mistakes...
> > Im a novice).
[quoted text clipped - 47 lines]
> That depends on the DBMS. dataDefinitionIgnoredInTransactions() in DatabaseMetaData
> might tell you the situation.

Thanks for your reply Lee. It is helpful and very much appreciated.

El.


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



©2009 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.