Test attached

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Bug_H2_3 {

    static String dbUrl = "jdbc:h2:tcp://localhost:9002/nioMemFS:db;DB_CLOSE_DELAY=-1;MULTI_THREADED=1"; //"jdbc:h2:nioMemFS:db;MULTI_THREADED=1";
    static String user = "sa", pwd = "";
    static int threadCount = 100;

    public static void main(String args[]) throws InterruptedException, SQLException {
        if (args.length > 0) {
            dbUrl = args[0];
        }
        Arte3[] artes = new Arte3[threadCount];
        Connection dbConnect;
        try {
            Class.forName("org.h2.Driver");
            dbConnect = DriverManager.getConnection(dbUrl, user, pwd);
            DbPreparator3.prepareScheme(dbConnect);
            System.out.println("DB scheme prepared");

            for (int i = 0; i < threadCount; i++) {
                artes[i] = new Arte3(DriverManager.getConnection(dbUrl, user, pwd), i);
            }
            System.out.println("ARTEs created");
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("DB Connection Failed: " + dbUrl);
            e.printStackTrace();
            return;
        }

        ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
        for (int i = 0; i < threadCount; i++) {
            threadPool.submit(artes[i]);
        }

        long t = System.currentTimeMillis();
        long lastTranCnt = 0;
        while (true) {
            Thread.sleep(100);
            long insertCnt = 0;
            for (int i = 0; i < threadCount; i++) {
                insertCnt += artes[i].insertCnt;
            }
            long t1 = System.currentTimeMillis();
            if (t1 - t > 1000) {
                System.out.println("TPS: " + ((int) ((insertCnt - lastTranCnt) * 1000.0 / (t1 - t)) + "; Inserts: " + insertCnt));
                lastTranCnt = insertCnt;
                t = t1;
            }
        }
    }
}

class DbPreparator3 {

    public static final int OBJ_CNT = 10000;
    private static final String[] SQLs = new String[]{
        "CREATE TABLE IF NOT EXISTS DOER ("
        + "     ID NUMBER(18,0) not null, \n"
        + "     SEQ NUMBER(9,0) not null, \n"
        + " PRIMARY KEY(ID, SEQ) \n"
        + ")"
    };

    public static void prepareScheme(Connection db) throws SQLException {
        for (String sql : SQLs) {
            db.createStatement().execute(sql);
            db.commit();
        }
    }

}

class Arte3 implements Callable<Integer> {

    Connection db;
    PreparedStatement insertDoerStmt;
    long tranId;
    int insertCnt;

    public Arte3(Connection db_, int number) throws SQLException {
        db = db_;
        db.setAutoCommit(false);
        insertDoerStmt = db.prepareStatement("INSERT INTO doer (id, seq) values(?, ?)");
        tranId = number * 1_000_000_000_000L; //to guarantee uniques
    }

    @Override
    public Integer call() {
        try {
            while (true) {
                for (int i = 0; i < 10; i++) {
                    insertDoerStmt.setLong(1, tranId);
                    insertDoerStmt.setLong(2, i);
                    insertDoerStmt.addBatch();
                    insertCnt++;
                }
                insertDoerStmt.executeBatch();
                db.commit();
                tranId++;
            }
        } catch (SQLException e) {
            System.out.println("Insert error at " + tranId + ": ");
            e.printStackTrace();
            System.exit(0);
        }
        return null;
    }
}

Reply via email to