sjeiwei / sqljet

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

Corrupt database when remove and Insert many rows #156

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Insert many rows
Remove many rows
And insert others many rows

This is the main class :

---------------------------------------------------------------------
package old;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
import org.tmatesoft.sqljet.core.table.ISqlJetCursor;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;

public class TestBase {

    /**
     * @param args
     * @throws SqlJetException 
     */
    public static void main(String[] args) throws SqlJetException {

        final File dbFile = new File("db.sqlfile");

        createDatabase(dbFile);
        addRows(dbFile);
        removeAllRows(dbFile);
        addRows(dbFile);
    }

    private static void createDatabase(final File dbFile) throws SqlJetException {
        // Open Connection
        SqlJetDb db = SqlJetDb.open(dbFile, true);
        db.beginTransaction(SqlJetTransactionMode.EXCLUSIVE);

        // Create Table
        db.createTable("CREATE TABLE test (" +
                "    pk INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
        "    preview BLOB)");

        // Commit & close
        db.commit();
        db.close();
    }

    private static void removeAllRows(final File dbFile) throws SqlJetException {
        // Open Connection
        SqlJetDb db = SqlJetDb.open(dbFile, true);
        db.beginTransaction(SqlJetTransactionMode.EXCLUSIVE);

        // Remove all rows
        ISqlJetCursor curseur = null;
        final ISqlJetTable tableDossiers = db.getTable("test");
        curseur = tableDossiers.open();
        while (!curseur.eof()) {
            curseur.delete();
        }
        curseur.close();

        // Commit & close
        db.commit();
        db.close();
    }

    private static void addRows(final File dbFile) throws SqlJetException {
        final int rowCount = 100;

        // Open Connection
        SqlJetDb db = SqlJetDb.open(dbFile, true);
        db.beginTransaction(SqlJetTransactionMode.EXCLUSIVE);

        // Create X rows
        final ISqlJetTable testTable = db.getTable("test");
        for (int i=0; i<rowCount; i++) {
            final Map<String, Object> values = new HashMap<String, Object>();
            values.put("preview", createByte()); 

            final long pkTestTable = testTable.insertByFieldNames(values);
            System.out.println("PK : " + pkTestTable);
        }

        // Commit & Close
        db.commit();
        db.close();
    }

    private static byte[] createByte() {
            final int taille = 15000;
            byte[] tab = new byte[taille];
            Random r = new Random();
            r.nextBytes(tab);
            return tab;
    }

}
--------------------------------------------------------------

The Output execution is :

PK : 1
PK : 2
...
PK : 99
PK : 100
PK : 1
PK : 2
...
PK : 15
PK : 16
Exception in thread "main" org.tmatesoft.sqljet.core.SqlJetException: CORRUPT: 
error code is CORRUPT
    at org.tmatesoft.sqljet.core.internal.pager.SqlJetPager.acquirePage(SqlJetPager.java:1011)
    at org.tmatesoft.sqljet.core.internal.btree.SqlJetBtreeShared.getPage(SqlJetBtreeShared.java:339)
    at org.tmatesoft.sqljet.core.internal.btree.SqlJetBtreeShared.allocatePage(SqlJetBtreeShared.java:422)
    at org.tmatesoft.sqljet.core.internal.btree.SqlJetMemPage.fillInCell(SqlJetMemPage.java:1247)
    at org.tmatesoft.sqljet.core.internal.btree.SqlJetBtreeCursor.insert(SqlJetBtreeCursor.java:1864)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetBtreeDataTable.doInsert(SqlJetBtreeDataTable.java:413)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetBtreeDataTable.insertWithRowId(SqlJetBtreeDataTable.java:204)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetBtreeDataTable.insert(SqlJetBtreeDataTable.java:909)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetTable$5.run(SqlJetTable.java:192)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetTable$3.run(SqlJetTable.java:160)
    at org.tmatesoft.sqljet.core.table.SqlJetDb$6.runWithLock(SqlJetDb.java:399)
    at org.tmatesoft.sqljet.core.table.SqlJetDb.runWithLock(SqlJetDb.java:305)
    at org.tmatesoft.sqljet.core.table.SqlJetDb.runTransaction(SqlJetDb.java:393)
    at org.tmatesoft.sqljet.core.table.SqlJetDb.runWriteTransaction(SqlJetDb.java:365)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetTable.runWriteTransaction(SqlJetTable.java:156)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetTable.insertByFieldNamesOr(SqlJetTable.java:190)
    at org.tmatesoft.sqljet.core.internal.table.SqlJetTable.insertByFieldNames(SqlJetTable.java:173)
    at old.TestBase.addRows(TestBase.java:80)
    at old.TestBase.main(TestBase.java:27)

It is the same result if I keep the same connection.

Original issue reported on code.google.com by denis.r...@gmail.com on 2 Jun 2011 at 10:46

GoogleCodeExporter commented 9 years ago
I use the version sqljet-1.0.5 on Ubuntu 10.10 with OpenJDK 1.6.0_20.

Original comment by denis.r...@gmail.com on 2 Jun 2011 at 10:48

GoogleCodeExporter commented 9 years ago

Original comment by sergey.s...@gmail.com on 2 Jun 2011 at 10:55

GoogleCodeExporter commented 9 years ago
Thanks for source code. Issue is fixed in trunk at revision 1119.

Original comment by sergey.s...@gmail.com on 8 Jun 2011 at 9:54