3
我試圖在多線程的H2上插入更好的性能。減少H2上的線程爭用
我試圖四個不同的設置,每一個具有20個並行線程執行插入:
- 共用連接;
- 禁用自動提交的共享連接;
- 連接池;
- 禁止自動提交的連接池。
得到了最好的結果與選項4:創建
表:
create table foo (id int not null auto_increment primary key, name varchar(50))
代碼插件(c
是從org.h2.jdbcx.JdbcConnectionPool
的連接):
private static void insertDataWithConnection(Connection c, String n) throws SQLException {
c.setAutoCommit(false);
PreparedStatement ps = c.prepareStatement("insert into foo (name) values (?)");
ps.setString(1, n);
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
rs.next();
rs.getInt(1);
rs.close();
c.commit();
c.setAutoCommit(true);
ps.close();
}
連接字符串:
jdbc:h2:test;AUTOCOMMIT=OFF;WRITE_DELAY=300;MVCC=TRUE;LOCK_MODE=0;FILE_LOCK=SOCKET
是否有可能減少線程爭不知何故?
P.S:
- 獲取生成的密鑰是非常重要的。
- 將H2切換到另一個嵌入式數據庫可能是一個選項;但是,請您將答案集中在H2上。
我已經使用MVCC。將連接字符串添加到問題中。 –
您是否嘗試使用多線程選項? http://www.h2database.com/html/grammar.html#set_multi_threaded –
這就是我在我的答案 –