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