use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testPrepareStatement_StringArray_Execute.
/**
* Test method for
* {@link Connection#prepareStatement(String, String[])}
* .{@link PreparedStatement#execute()}.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testPrepareStatement_StringArray_Execute(Connection conn) throws Exception {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (10)", new String[0]);
prep.executeUpdate();
ResultSet rs = prep.getGeneratedKeys();
assertFalse(rs.next());
rs.close();
prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (20)", new String[] { "ID", "UID" });
prep.execute();
rs = prep.getGeneratedKeys();
assertEquals(2, rs.getMetaData().getColumnCount());
assertEquals("ID", rs.getMetaData().getColumnName(1));
assertEquals("UID", rs.getMetaData().getColumnName(2));
assertTrue(rs.next());
assertEquals(2L, rs.getLong(1));
assertEquals(UUID.class, rs.getObject(2).getClass());
assertFalse(rs.next());
rs.close();
prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (30)", new String[] { "UID", "ID" });
prep.execute();
rs = prep.getGeneratedKeys();
assertEquals(2, rs.getMetaData().getColumnCount());
assertEquals("UID", rs.getMetaData().getColumnName(1));
assertEquals("ID", rs.getMetaData().getColumnName(2));
assertTrue(rs.next());
assertEquals(UUID.class, rs.getObject(1).getClass());
assertEquals(3L, rs.getLong(2));
assertFalse(rs.next());
rs.close();
prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (40)", new String[] { "UID" });
prep.execute();
rs = prep.getGeneratedKeys();
assertEquals(1, rs.getMetaData().getColumnCount());
assertEquals("UID", rs.getMetaData().getColumnName(1));
assertTrue(rs.next());
assertEquals(UUID.class, rs.getObject(1).getClass());
assertFalse(rs.next());
rs.close();
stat.execute("DROP TABLE TEST");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestJavaObjectSerializer method testStaticGlobalSerializer.
private void testStaticGlobalSerializer() throws Exception {
JdbcUtils.serializer = new JavaObjectSerializer() {
@Override
public byte[] serialize(Object obj) throws Exception {
assertEquals(100500, ((Integer) obj).intValue());
return new byte[] { 1, 2, 3 };
}
@Override
public Object deserialize(byte[] bytes) throws Exception {
assertEquals(new byte[] { 1, 2, 3 }, bytes);
return 100500;
}
};
try {
deleteDb("javaSerializer");
Connection conn = getConnection("javaSerializer");
Statement stat = conn.createStatement();
stat.execute("create table t(id identity, val other)");
PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)");
ins.setObject(1, 100500, Types.JAVA_OBJECT);
assertEquals(1, ins.executeUpdate());
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select val from t");
assertTrue(rs.next());
assertEquals(100500, ((Integer) rs.getObject(1)).intValue());
assertEquals(new byte[] { 1, 2, 3 }, rs.getBytes(1));
conn.close();
deleteDb("javaSerializer");
} finally {
JdbcUtils.serializer = null;
}
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestXA method testRollbackAfterPrepare.
private void testRollbackAfterPrepare() throws Exception {
if (config.memory) {
return;
}
Xid xid = new Xid() {
@Override
public int getFormatId() {
return 3145;
}
@Override
public byte[] getGlobalTransactionId() {
return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
}
@Override
public byte[] getBranchQualifier() {
return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
}
};
deleteDb("xa");
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(getURL("xa", true));
ds.setPassword(getPassword());
Connection dm = ds.getConnection();
Statement stat = dm.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
dm.close();
XAConnection c = ds.getXAConnection();
XAResource xa = c.getXAResource();
Connection connection = c.getConnection();
xa.start(xid, XAResource.TMJOIN);
PreparedStatement ps = connection.prepareStatement("UPDATE TEST SET VAL=? WHERE ID=?");
ps.setInt(1, new Random().nextInt());
ps.setInt(2, 1);
ps.close();
xa.prepare(xid);
xa.rollback(xid);
connection.close();
c.close();
deleteDb("xa");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestXA method testRollbackWithoutPrepare.
private void testRollbackWithoutPrepare() throws Exception {
if (config.memory) {
return;
}
Xid xid = new Xid() {
@Override
public int getFormatId() {
return 3145;
}
@Override
public byte[] getGlobalTransactionId() {
return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
}
@Override
public byte[] getBranchQualifier() {
return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
}
};
deleteDb("xa");
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(getURL("xa", true));
ds.setPassword(getPassword());
Connection dm = ds.getConnection();
Statement stat = dm.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
dm.close();
XAConnection c = ds.getXAConnection();
XAResource xa = c.getXAResource();
Connection connection = c.getConnection();
xa.start(xid, XAResource.TMJOIN);
PreparedStatement ps = connection.prepareStatement("UPDATE TEST SET VAL=? WHERE ID=?");
ps.setInt(1, new Random().nextInt());
ps.setInt(2, 1);
ps.close();
xa.rollback(xid);
connection.close();
c.close();
deleteDb("xa");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestMvccMultiThreaded method testConcurrentSelectForUpdate.
private void testConcurrentSelectForUpdate() throws Exception {
deleteDb(getTestName());
Connection conn = getConnection(getTestName() + ";MULTI_THREADED=TRUE");
Statement stat = conn.createStatement();
stat.execute("create table test(id int not null primary key, updated int not null)");
stat.execute("insert into test(id, updated) values(1, 100)");
ArrayList<Task> tasks = new ArrayList<>();
int count = 3;
for (int i = 0; i < count; i++) {
Task task = new Task() {
@Override
public void call() throws Exception {
Connection conn = getConnection(getTestName());
Statement stat = conn.createStatement();
try {
while (!stop) {
try {
stat.execute("select * from test where id=1 for update");
} catch (SQLException e) {
int errorCode = e.getErrorCode();
assertTrue(e.getMessage(), errorCode == ErrorCode.DEADLOCK_1 || errorCode == ErrorCode.LOCK_TIMEOUT_1);
}
}
} finally {
conn.close();
}
}
}.execute();
tasks.add(task);
}
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
ResultSet rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
}
for (Task t : tasks) {
t.get();
}
conn.close();
deleteDb(getTestName());
}
Aggregations