use of org.h2.command.dml.Delete in project h2database by h2database.
the class TestTools method testRecover.
private void testRecover() throws SQLException {
if (config.memory) {
return;
}
deleteDb("toolsRecover");
org.h2.Driver.load();
String url = getURL("toolsRecover", true);
Connection conn = getConnection(url, "sa", "sa");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, " + "name varchar, b blob, c clob)");
stat.execute("create table \"test 2\"(id int primary key, name varchar)");
stat.execute("comment on table test is ';-)'");
stat.execute("insert into test values" + "(1, 'Hello', SECURE_RAND(4100), '\u00e4' || space(4100))");
ResultSet rs;
rs = stat.executeQuery("select * from test");
rs.next();
byte[] b1 = rs.getBytes(3);
String s1 = rs.getString(4);
conn.close();
Recover.main("-dir", getBaseDir(), "-db", "toolsRecover");
// deleteDb would delete the .lob.db directory as well
// deleteDb("toolsRecover");
ArrayList<String> list = FileLister.getDatabaseFiles(getBaseDir(), "toolsRecover", true);
for (String fileName : list) {
if (!FileUtils.isDirectory(fileName)) {
FileUtils.delete(fileName);
}
}
conn = getConnection(url);
stat = conn.createStatement();
String suffix = ".h2.sql";
stat.execute("runscript from '" + getBaseDir() + "/toolsRecover" + suffix + "'");
rs = stat.executeQuery("select * from \"test 2\"");
assertFalse(rs.next());
rs = stat.executeQuery("select * from test");
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2));
byte[] b2 = rs.getBytes(3);
String s2 = rs.getString(4);
assertEquals("\u00e4 ", s2.substring(0, 2));
assertEquals(4100, b2.length);
assertEquals(4101, s2.length());
assertEquals(b1, b2);
assertEquals(s1, s2);
assertFalse(rs.next());
conn.close();
deleteDb("toolsRecover");
FileUtils.delete(getBaseDir() + "/toolsRecover.h2.sql");
String dir = getBaseDir() + "/toolsRecover.lobs.db";
FileUtils.deleteRecursive(dir, false);
}
use of org.h2.command.dml.Delete in project narayana by jbosstm.
the class TestCommitMarkableResourceFailActivate method testFailAfterPrepare.
@Test
@BMScript("commitMarkableResourceFailAfterCommit")
public void testFailAfterPrepare() throws Exception {
final DataSource dataSource = new JdbcDataSource();
((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
// Test code
Utils.createTables(dataSource.getConnection());
// We can't just instantiate one as we need to be using the
// same one as
// the transaction
// manager would have used to mark the transaction for GC
CommitMarkableResourceRecordRecoveryModule recoveryModule = null;
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
recoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
} else if (m instanceof XARecoveryModule) {
XARecoveryModule xarm = (XARecoveryModule) m;
xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
public boolean initialise(String p) throws Exception {
return true;
}
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { xaResource };
}
});
}
}
}
// final Object o = new Object();
// synchronized (o) {
Thread foo = new Thread(new Runnable() {
public void run() {
try {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
Connection localJDBCConnection = dataSource.getConnection();
localJDBCConnection.setAutoCommit(false);
nonXAResource = new JDBCConnectableResource(localJDBCConnection);
tm.getTransaction().enlistResource(nonXAResource);
xaResource = new SimpleXAResource();
tm.getTransaction().enlistResource(xaResource);
localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
tm.commit();
} catch (ExecuteException t) {
} catch (Exception t) {
t.printStackTrace();
failed = true;
} catch (Error t) {
}
}
});
foo.start();
foo.join();
assertFalse(failed);
// Now we need to correctly complete the transaction
manager.scan();
assertFalse(xaResource.wasCommitted());
assertFalse(xaResource.wasRolledback());
// This is test code, it allows us to verify that the
// correct XID was
// removed
Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
assertNotNull(committed);
// The recovery module has to perform lookups
new InitialContext().rebind("commitmarkableresource", dataSource);
// Run the first pass it will load the committed Xids into so we can
// indepently verify that the item was committed
recoveryModule.periodicWorkFirstPass();
recoveryModule.periodicWorkSecondPass();
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
// Run the scan to clear the content
manager.scan();
assertTrue(xaResource.wasCommitted());
assertFalse(xaResource.wasRolledback());
// Make sure that the resource was GC'd by the CRRRM
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
recoveryModule.periodicWorkFirstPass();
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
// This is the pass that will delete it
recoveryModule.periodicWorkSecondPass();
assertFalse(recoveryModule.wasCommitted("commitmarkableresource", committed));
}
Aggregations