001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.io.File;
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.Statement;
012: import java.util.ArrayList;
013:
014: import org.h2.constant.SysProperties;
015: import org.h2.store.FileLister;
016: import org.h2.test.TestBase;
017:
018: /**
019: * Tests the database transaction log file.
020: */
021: public class TestLogFile extends TestBase {
022:
023: private Connection conn;
024: private static final int MAX_LOG_SIZE = 1;
025:
026: private long reconnect(int maxFiles) throws Exception {
027: if (conn != null) {
028: conn.close();
029: }
030: long length = 0;
031: ArrayList files = FileLister.getDatabaseFiles(baseDir,
032: "logfile", false);
033: checkSmaller(files.size(), maxFiles + 2);
034: for (int i = 0; i < files.size(); i++) {
035: String fileName = (String) files.get(i);
036: long len = new File(fileName).length();
037: length += len;
038: }
039: conn = getConnection("logfile");
040: return length;
041: }
042:
043: public void test() throws Exception {
044: if (config.memory) {
045: return;
046: }
047: deleteDb("logfile");
048: int old = SysProperties.getLogFileDeleteDelay();
049: System.setProperty(SysProperties.H2_LOG_DELETE_DELAY, "0");
050: try {
051: reconnect(0);
052: insert();
053: int maxFiles = 3; // data, index, log
054: for (int i = 0; i < 3; i++) {
055: long length = reconnect(maxFiles);
056: insert();
057: long l2 = reconnect(maxFiles);
058: trace("length:" + length + " l2:" + l2);
059: check(l2 <= length * 2);
060: }
061: conn.close();
062: } finally {
063: System.setProperty(SysProperties.H2_LOG_DELETE_DELAY, ""
064: + old);
065: }
066: }
067:
068: private void checkLogSize() throws Exception {
069: String[] files = new File(".").list();
070: for (int j = 0; j < files.length; j++) {
071: String name = files[j];
072: if (name.startsWith("logfile") && name.endsWith(".log.db")) {
073: long length = new File(name).length();
074: checkSmaller(length, MAX_LOG_SIZE * 1024 * 1024 * 2);
075: }
076: }
077: }
078:
079: void insert() throws Exception {
080: Statement stat = conn.createStatement();
081: stat.execute("SET LOGSIZE 200");
082: stat.execute("SET MAX_LOG_SIZE " + MAX_LOG_SIZE);
083: stat.execute("DROP TABLE IF EXISTS TEST");
084: stat
085: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
086: PreparedStatement prep = conn
087: .prepareStatement("INSERT INTO TEST VALUES(?, 'Hello' || ?)");
088: int len = getSize(1, 10000);
089: for (int i = 0; i < len; i++) {
090: prep.setInt(1, i);
091: prep.setInt(2, i);
092: prep.execute();
093: if (i > 0 && (i % 2000) == 0) {
094: checkLogSize();
095: }
096: }
097: checkLogSize();
098: // stat.execute("TRUNCATE TABLE TEST");
099: }
100:
101: }
|