Subject | Re: Blobs and batches |
---|---|
Author | Serge Bogatyrev |
Post date | 2009-08-28T14:43:22Z |
...probably, attachments are not supported.
-------------------------------------------------------------
package error.sample.batch;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import org.firebirdsql.management.FBManager;
import org.junit.Test;
/**
* @author Serge Bogatyrev
*/
public class BatchTest {
private static final String INSERT_SQL = "insert into foo (id, name,
note) values (?, ?, ?)";
private static final String CREATE_TABLE_SQL = "create table foo (id
int not null primary key,"
+ "name varchar(100) not null, note blob)";
private static final String DROP_TABLE_SQL = "drop table foo";
@Test
public void test() throws Exception {
Connection c = prepareDatabaseAndConnect();
try {
// Prepare and use statement
PreparedStatement insert = c.prepareStatement(INSERT_SQL);
for (int i = 0; i < 100; i++) {
insert.setInt(1, i);
insert.setString(2, String.valueOf(i));
insert.setString(3, UUID.randomUUID().toString());
insert.addBatch();
}
insert.executeBatch();
// This commit is important for the first error.
c.commit();
// "335544332. invalid transaction handle" will be throw in loop.
// To get next error "335544329. invalid BLOB ID" uncomment the following
// line.
// c.prepareStatement("select 1 from rdb$database");
// Here we are using the same statement.
for (int i = 100; i < 102; i++) {
insert.setInt(1, i);
insert.setString(2, String.valueOf(i));
insert.setString(3, UUID.randomUUID().toString());
insert.addBatch();
}
insert.executeBatch();
insert.close();
} finally {
c.close();
}
}
private Connection prepareDatabaseAndConnect() throws Exception,
ClassNotFoundException,
SQLException {
String user = "sysdba";
String password = "masterkey";
File dbFile = new File(System.getProperty("java.io.tmpdir"), "batch.fdb");
FBManager m = new FBManager("PURE_JAVA");
m.start();
m.createDatabase(dbFile.getPath(), user, password);
m.stop();
Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection c = DriverManager.getConnection("jdbc:firebirdsql:localhost:"
+ dbFile.getAbsolutePath(), user, password);
c.setAutoCommit(true);
Statement stm = c.createStatement();
try {
stm.executeUpdate(DROP_TABLE_SQL);
} catch (Exception e) {
}
stm.executeUpdate(CREATE_TABLE_SQL);
stm.close();
c.setAutoCommit(false);
return c;
}
}
-------------------------------------------------------------
On Fri, Aug 28, 2009 at 3:49 PM, Serge
Bogatyrev<sergebogatyrjov@...> wrote:
-------------------------------------------------------------
package error.sample.batch;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import org.firebirdsql.management.FBManager;
import org.junit.Test;
/**
* @author Serge Bogatyrev
*/
public class BatchTest {
private static final String INSERT_SQL = "insert into foo (id, name,
note) values (?, ?, ?)";
private static final String CREATE_TABLE_SQL = "create table foo (id
int not null primary key,"
+ "name varchar(100) not null, note blob)";
private static final String DROP_TABLE_SQL = "drop table foo";
@Test
public void test() throws Exception {
Connection c = prepareDatabaseAndConnect();
try {
// Prepare and use statement
PreparedStatement insert = c.prepareStatement(INSERT_SQL);
for (int i = 0; i < 100; i++) {
insert.setInt(1, i);
insert.setString(2, String.valueOf(i));
insert.setString(3, UUID.randomUUID().toString());
insert.addBatch();
}
insert.executeBatch();
// This commit is important for the first error.
c.commit();
// "335544332. invalid transaction handle" will be throw in loop.
// To get next error "335544329. invalid BLOB ID" uncomment the following
// line.
// c.prepareStatement("select 1 from rdb$database");
// Here we are using the same statement.
for (int i = 100; i < 102; i++) {
insert.setInt(1, i);
insert.setString(2, String.valueOf(i));
insert.setString(3, UUID.randomUUID().toString());
insert.addBatch();
}
insert.executeBatch();
insert.close();
} finally {
c.close();
}
}
private Connection prepareDatabaseAndConnect() throws Exception,
ClassNotFoundException,
SQLException {
String user = "sysdba";
String password = "masterkey";
File dbFile = new File(System.getProperty("java.io.tmpdir"), "batch.fdb");
FBManager m = new FBManager("PURE_JAVA");
m.start();
m.createDatabase(dbFile.getPath(), user, password);
m.stop();
Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection c = DriverManager.getConnection("jdbc:firebirdsql:localhost:"
+ dbFile.getAbsolutePath(), user, password);
c.setAutoCommit(true);
Statement stm = c.createStatement();
try {
stm.executeUpdate(DROP_TABLE_SQL);
} catch (Exception e) {
}
stm.executeUpdate(CREATE_TABLE_SQL);
stm.close();
c.setAutoCommit(false);
return c;
}
}
-------------------------------------------------------------
On Fri, Aug 28, 2009 at 3:49 PM, Serge
Bogatyrev<sergebogatyrjov@...> wrote:
> Hi, all
>
>
> I tryed to use batches and discovered a chain of bugs.
>
> I am using connection pool with (obviously) statement caching. All
> errors occur when a prepared statement is used in context of new
> transaction. And all works fine when I replacing the "blob" with
> "varchar(100)".
>
> All bugs are described in the test.
>
>
> Serge Bogatyrev.
>