Search in sources :

Example 31 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class MVTable method addRow.

@Override
public void addRow(Session session, Row row) {
    lastModificationId = database.getNextModificationDataId();
    Transaction t = session.getTransaction();
    long savepoint = t.setSavepoint();
    try {
        for (Index index : indexes) {
            index.add(session, row);
        }
    } catch (Throwable e) {
        t.rollbackToSavepoint(savepoint);
        DbException de = DbException.convert(e);
        if (de.getErrorCode() == ErrorCode.DUPLICATE_KEY_1) {
            for (Index index : indexes) {
                if (index.getIndexType().isUnique() && index instanceof MultiVersionIndex) {
                    MultiVersionIndex mv = (MultiVersionIndex) index;
                    if (mv.isUncommittedFromOtherSession(session, row)) {
                        throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, index.getName());
                    }
                }
            }
        }
        throw de;
    }
    analyzeIfRequired(session);
}
Also used : Transaction(org.h2.mvstore.db.TransactionStore.Transaction) MultiVersionIndex(org.h2.index.MultiVersionIndex) Index(org.h2.index.Index) MultiVersionIndex(org.h2.index.MultiVersionIndex) DbException(org.h2.message.DbException)

Example 32 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class MVTable method removeRow.

@Override
public void removeRow(Session session, Row row) {
    lastModificationId = database.getNextModificationDataId();
    Transaction t = session.getTransaction();
    long savepoint = t.setSavepoint();
    try {
        for (int i = indexes.size() - 1; i >= 0; i--) {
            Index index = indexes.get(i);
            index.remove(session, row);
        }
    } catch (Throwable e) {
        t.rollbackToSavepoint(savepoint);
        throw DbException.convert(e);
    }
    analyzeIfRequired(session);
}
Also used : Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Index(org.h2.index.Index) MultiVersionIndex(org.h2.index.MultiVersionIndex) Constraint(org.h2.constraint.Constraint)

Example 33 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class MVPrimaryIndex method remove.

@Override
public void remove(Session session) {
    TransactionMap<Value, Value> map = getMap(session);
    if (!map.isClosed()) {
        Transaction t = session.getTransaction();
        t.removeMap(map);
    }
}
Also used : Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Value(org.h2.value.Value)

Example 34 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class MVSpatialIndex method remove.

@Override
public void remove(Session session) {
    TransactionMap<SpatialKey, Value> map = getMap(session);
    if (!map.isClosed()) {
        Transaction t = session.getTransaction();
        t.removeMap(map);
    }
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Value(org.h2.value.Value) VersionedValue(org.h2.mvstore.db.TransactionStore.VersionedValue)

Example 35 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class TestCluster method testCreateClusterAtRuntime.

private void testCreateClusterAtRuntime() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    int len = 10;
    // initialize the database
    Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = n1.getPort();
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
    conn = getConnection(url1, user, password);
    stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar) as " + "select x, 'Data' || x from system_range(0, " + (len - 1) + ")");
    stat.execute("create user test password 'test'");
    stat.execute("grant all on test to test");
    // start the second server
    Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = n2.getPort();
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
    // copy the database and initialize the cluster
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    // check the original connection is closed
    assertThrows(ErrorCode.CONNECTION_BROKEN_1, stat).execute("select * from test");
    JdbcUtils.closeSilently(conn);
    // test the cluster connection
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", false);
    Connection connApp = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password);
    check(connApp, len, "'" + serverList + "'");
    // delete the rows, but don't commit
    connApp.setAutoCommit(false);
    connApp.createStatement().execute("delete from test");
    // stop server 2, and test if only one server is available
    n2.stop();
    // rollback the transaction
    connApp.createStatement().executeQuery("select count(*) from test");
    connApp.rollback();
    check(connApp, len, "''");
    connApp.setAutoCommit(true);
    // re-create the cluster
    n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    // test the cluster connection
    check(connApp, len, "'" + serverList + "'");
    connApp.close();
    // test a non-admin user
    String user2 = "test", password2 = getPassword("test");
    connApp = getConnection(urlCluster, user2, password2);
    check(connApp, len, "'" + serverList + "'");
    connApp.close();
    n1.stop();
    // test non-admin cluster connection if only one server runs
    Connection connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();
    // test non-admin cluster connection if only one server runs
    connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();
    n2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Transaction (org.h2.mvstore.db.TransactionStore.Transaction)22 MVStore (org.h2.mvstore.MVStore)18 TransactionStore (org.h2.mvstore.db.TransactionStore)18 Connection (java.sql.Connection)13 ResultSet (java.sql.ResultSet)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)8 DbException (org.h2.message.DbException)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 PreparedStatement (java.sql.PreparedStatement)7 Statement (java.sql.Statement)7 Enumeration (java.util.Enumeration)7 XAResource (javax.transaction.xa.XAResource)7 Test (org.junit.Test)7 SQLException (java.sql.SQLException)6 Vector (java.util.Vector)6 Constraint (org.h2.constraint.Constraint)6