google-code-export / h2database

Automatically exported from code.google.com/p/h2database
0 stars 1 forks source link

Inserting data in several threads to nioMapped database fails ... #312

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Before submitting a bug, please check the FAQ:
http://www.h2database.com/html/faq.html

What steps will reproduce the problem?
(simple SQL scripts or simple standalone applications are preferred)

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class TestCsvRead {
    public static void main(String[] args) throws SQLException, IOException, InterruptedException {
        int recordCount = 1000000;
        int threadCount = 5;
        for(int j = 0;j<threadCount;j++) {
          BufferedWriter bw = new BufferedWriter(new FileWriter("c:/TESTTABLE"+j+".csv"));
          bw.write("#COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13, COL14\n");
          for(int i = 0;i<recordCount/2;i++) {
              bw.write("62.2,39.2,56.3,110.6,77.8,94,86,63.9,64.6,95.3,68,702,3,2010-12-01\n");
              bw.write("61.4,67.1,52.6,103.1,50,77.2,51,81.8,71.4,47.6,19.4,714,1,2010-12-01\n");
          }
          bw.close();
        }

        final String url0 = "jdbc:h2:nioMapped:c:/testcsv;LOG=0;CACHE_SIZE=65536;LOCK_MODE=0;UNDO_LOG=0;MULTI_THREADED=1";
        final String url = "jdbc:h2:nioMapped:c:/testcsv";
        Connection conn = DriverManager.getConnection(url0);
        Statement stmt = conn.createStatement();

        for(int i = 0;i<threadCount;i++) {
            stmt.execute("DROP TABLE IF EXISTS TESTTABLE"+i);
        }
        TC tc = new TC();
        List<Thread> threads = new ArrayList<Thread>();
        for (int i = 0; i < threadCount; i++) {
            final int fi = i;
            Thread e = new Thread() {
                @Override public void run() {
                    try {
                        Connection conn = DriverManager.getConnection(url);
                        Statement stmt = conn.createStatement();
                        stmt.execute("CREATE TABLE TESTTABLE"+fi+"(  COL1 DOUBLE,    COL2 DOUBLE,    COL3 DOUBLE, COL4 DOUBLE,    COL5 DOUBLE,    COL6 DOUBLE,    COL7 DOUBLE,    COL8  DOUBLE, COL9 DOUBLE,    COL10 DOUBLE, COL11 DOUBLE, COL12 INT, COL13 INT, COL14 TIMESTAMP) AS SELECT * FROM CSVREAD('c:/TESTTABLE"+fi+".csv','COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13,COL1','lineComment=#');");
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            };
            e.start();
            threads.add(e);
        }

        for (int i = 0; i < threadCount; i++) {
            threads.get(i).join();
        }

        stmt.execute("SHUTDOWN");
        tc.stop("csvread", recordCount*threadCount);
    }

What is the expected output? What do you see instead?

org.h2.jdbc.JdbcSQLException: Obecná chyba: "java.lang.NullPointerException"
General error: "java.lang.NullPointerException"; SQL statement:
CREATE TABLE TESTTABLE3(  COL1 DOUBLE,    COL2 DOUBLE,    COL3 DOUBLE, COL4 
DOUBLE,    COL5 DOUBLE,    COL6 DOUBLE,    COL7 DOUBLE,    COL8  DOUBLE, COL9 
DOUBLE,    COL10 DOUBLE, COL11 DOUBLE, COL12 INT, COL13 INT, COL14 TIMESTAMP) 
AS SELECT * FROM CSVREAD('c:/TESTTABLE3.csv','COL1, COL2, COL3, COL4, COL5, 
COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13,COL1','lineComment=#'); 
[50000-153]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
    at org.h2.message.DbException.get(DbException.java:156)
    at org.h2.message.DbException.convert(DbException.java:279)
    at org.h2.table.RegularTable.removeRow(RegularTable.java:380)
    at org.h2.engine.Database.removeMeta(Database.java:726)
    at org.h2.table.Table.removeChildrenAndResources(Table.java:459)
    at org.h2.table.RegularTable.removeChildrenAndResources(RegularTable.java:650)
    at org.h2.engine.Database.removeSchemaObject(Database.java:1554)
    at org.h2.command.ddl.CreateTable.update(CreateTable.java:183)
    at org.h2.command.CommandContainer.update(CommandContainer.java:69)
    at org.h2.command.Command.executeUpdate(Command.java:212)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:177)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:152)
    at TestCsvRead$1.run(TestCsvRead.java:44)
Caused by: java.lang.NullPointerException
    at org.h2.store.fs.FileObjectDiskMapped.readFully(FileObjectDiskMapped.java:146)
    at org.h2.store.FileStore.readFully(FileStore.java:285)
    at org.h2.store.PageStore.readPage(PageStore.java:1270)
    at org.h2.store.PageStore.getPage(PageStore.java:706)
    at org.h2.index.PageDataIndex.getPage(PageDataIndex.java:222)
    at org.h2.index.PageDataIndex.remove(PageDataIndex.java:323)
    at org.h2.table.RegularTable.removeRow(RegularTable.java:362)
    ... 10 more

What version of the product are you using? On what operating system, file
system, and virtual machine?
h2-1.3.153/Win7x64/jdk1.6.0_24

Original issue reported on code.google.com by michal.b...@gmail.com on 15 Apr 2011 at 8:41

GoogleCodeExporter commented 9 years ago
Hi,

You have disabled locking (lock_mode 0) and used multi-threaded access. This is 
not supported, unfortunately the database didn't detect this. The next version 
of H2 will detect it and not allow it. I believe this is the reason why the 
database gets corrupt (because the file is accessed by multiple threads 
concurrently).

You you try again with either using the default lock mode, or disabling 
multi-threaded access?

Regards,
Thomas

Original comment by thomas.t...@gmail.com on 19 May 2011 at 6:10

GoogleCodeExporter commented 9 years ago
Trying to disable locking and multi-threaded is now detected and rejected.

Original comment by thomas.t...@gmail.com on 11 Sep 2011 at 3:02