提交 f8c24c4f authored 作者: Thomas Mueller's avatar Thomas Mueller

The .trace.db file is now only created if required.

上级 9ae1fcab
...@@ -18,7 +18,8 @@ Change Log ...@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Shell tool: improved PostgreSQL compatibility. <ul><li>The .trace.db file is now only created if required.
</li><li>Shell tool: improved PostgreSQL compatibility.
</li><li>Trying to open a database in read-only mode when a .lock.db file exists will now fail </li><li>Trying to open a database in read-only mode when a .lock.db file exists will now fail
with a nice error message. with a nice error message.
</li><li>H2 Console: data that is too long is now abbreviated as follows: text... (100000 characters). </li><li>H2 Console: data that is too long is now abbreviated as follows: text... (100000 characters).
......
...@@ -587,9 +587,9 @@ public class Database implements DataHandler { ...@@ -587,9 +587,9 @@ public class Database implements DataHandler {
} }
boolean exists = existsData || existsPage; boolean exists = existsData || existsPage;
if (readOnly) { if (readOnly) {
traceSystem = new TraceSystem(null, false); traceSystem = new TraceSystem(null);
} else { } else {
traceSystem = new TraceSystem(databaseName + Constants.SUFFIX_TRACE_FILE, true); traceSystem = new TraceSystem(databaseName + Constants.SUFFIX_TRACE_FILE);
} }
traceSystem.setLevelFile(traceLevelFile); traceSystem.setLevelFile(traceLevelFile);
traceSystem.setLevelSystemOut(traceLevelSystemOut); traceSystem.setLevelSystemOut(traceLevelSystemOut);
...@@ -656,7 +656,7 @@ public class Database implements DataHandler { ...@@ -656,7 +656,7 @@ public class Database implements DataHandler {
reserveLobFileObjectIds(); reserveLobFileObjectIds();
writer = WriterThread.create(this, writeDelay); writer = WriterThread.create(this, writeDelay);
} else { } else {
traceSystem = new TraceSystem(null, false); traceSystem = new TraceSystem(null);
log = new LogSystem(null, null, false, null, null); log = new LogSystem(null, null, false, null, null);
} }
systemUser = new User(this, 0, Constants.DBA_NAME, true); systemUser = new User(this, 0, Constants.DBA_NAME, true);
......
...@@ -681,7 +681,7 @@ public class Session extends SessionWithState { ...@@ -681,7 +681,7 @@ public class Session extends SessionWithState {
traceModuleName = Trace.JDBC + "[" + id + "]"; traceModuleName = Trace.JDBC + "[" + id + "]";
} }
if (closed) { if (closed) {
return new TraceSystem(null, false).getTrace(traceModuleName); return new TraceSystem(null).getTrace(traceModuleName);
} }
return database.getTrace(traceModuleName); return database.getTrace(traceModuleName);
} }
......
...@@ -276,7 +276,7 @@ public class SessionRemote extends SessionWithState implements SessionFactory, D ...@@ -276,7 +276,7 @@ public class SessionRemote extends SessionWithState implements SessionFactory, D
} }
databaseName = name.substring(idx + 1); databaseName = name.substring(idx + 1);
String server = name.substring(0, idx); String server = name.substring(0, idx);
traceSystem = new TraceSystem(null, false); traceSystem = new TraceSystem(null);
try { try {
String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null); String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null);
if (traceLevelFile != null) { if (traceLevelFile != null) {
......
...@@ -78,7 +78,7 @@ implements ObjectFactory ...@@ -78,7 +78,7 @@ implements ObjectFactory
private TraceSystem getTraceSystem() { private TraceSystem getTraceSystem() {
synchronized (JdbcDataSourceFactory.class) { synchronized (JdbcDataSourceFactory.class) {
if (cachedTraceSystem == null) { if (cachedTraceSystem == null) {
cachedTraceSystem = new TraceSystem(SysProperties.CLIENT_TRACE_DIRECTORY + "h2datasource" + Constants.SUFFIX_TRACE_FILE, false); cachedTraceSystem = new TraceSystem(SysProperties.CLIENT_TRACE_DIRECTORY + "h2datasource" + Constants.SUFFIX_TRACE_FILE);
cachedTraceSystem.setLevelFile(SysProperties.DATASOURCE_TRACE_LEVEL); cachedTraceSystem.setLevelFile(SysProperties.DATASOURCE_TRACE_LEVEL);
} }
return cachedTraceSystem; return cachedTraceSystem;
......
...@@ -100,20 +100,12 @@ public class TraceSystem implements TraceWriter { ...@@ -100,20 +100,12 @@ public class TraceSystem implements TraceWriter {
* Create a new trace system object. * Create a new trace system object.
* *
* @param fileName the file name * @param fileName the file name
* @param init if the trace system should be initialized
*/ */
public TraceSystem(String fileName, boolean init) { public TraceSystem(String fileName) {
this.fileName = fileName; this.fileName = fileName;
updateLevel(); updateLevel();
traces = SmallLRUCache.newInstance(100); traces = SmallLRUCache.newInstance(100);
dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss "); dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss ");
if (fileName != null && init) {
try {
openWriter();
} catch (Exception e) {
logWritingError(e);
}
}
} }
private void updateLevel() { private void updateLevel() {
...@@ -324,9 +316,6 @@ public class TraceSystem implements TraceWriter { ...@@ -324,9 +316,6 @@ public class TraceSystem implements TraceWriter {
} }
fileWriter = null; fileWriter = null;
} }
if (fileName != null && FileUtils.length(fileName) == 0) {
FileUtils.tryDelete(fileName);
}
} }
/** /**
......
...@@ -51,7 +51,7 @@ public class FileLister { ...@@ -51,7 +51,7 @@ public class FileLister {
public static void tryUnlockDatabase(ArrayList<String> files, String message) throws SQLException { public static void tryUnlockDatabase(ArrayList<String> files, String message) throws SQLException {
for (String fileName : files) { for (String fileName : files) {
if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) { if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
FileLock lock = new FileLock(new TraceSystem(null, false), fileName, Constants.LOCK_SLEEP); FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
try { try {
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLock.LOCK_FILE);
lock.unlock(); lock.unlock();
......
...@@ -400,7 +400,7 @@ public abstract class TestBase { ...@@ -400,7 +400,7 @@ public abstract class TestBase {
System.err.println("ERROR: " + s + " " + e.toString() + " ------------------------------"); System.err.println("ERROR: " + s + " " + e.toString() + " ------------------------------");
e.printStackTrace(); e.printStackTrace();
try { try {
TraceSystem ts = new TraceSystem(null, false); TraceSystem ts = new TraceSystem(null);
FileLock lock = new FileLock(ts, "error.lock", 1000); FileLock lock = new FileLock(ts, "error.lock", 1000);
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLock.LOCK_FILE);
FileWriter fw = new FileWriter("error.txt", true); FileWriter fw = new FileWriter("error.txt", true);
......
...@@ -57,10 +57,15 @@ public class TestStatement extends TestBase { ...@@ -57,10 +57,15 @@ public class TestStatement extends TestBase {
} }
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
String fileName = baseDir + "/statement.trace.db"; String fileName = baseDir + "/statement.trace.db";
FileObject trace = FileSystem.getInstance(fileName).openFileObject(fileName, "r");
stat.execute("DROP TABLE TEST IF EXISTS"); stat.execute("DROP TABLE TEST IF EXISTS");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY)"); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY)");
stat.execute("INSERT INTO TEST VALUES(1)"); stat.execute("INSERT INTO TEST VALUES(1)");
try {
stat.execute("ERROR");
} catch (SQLException e) {
// ignore
}
FileObject trace = FileSystem.getInstance(fileName).openFileObject(fileName, "r");
long start = trace.length(); long start = trace.length();
try { try {
stat.execute("ERROR"); stat.execute("ERROR");
......
...@@ -57,14 +57,14 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -57,14 +57,14 @@ public class TestFileLock extends TestBase implements Runnable {
f.delete(); f.delete();
f.createNewFile(); f.createNewFile();
f.setLastModified(System.currentTimeMillis() + 10000); f.setLastModified(System.currentTimeMillis() + 10000);
FileLock lock = new FileLock(new TraceSystem(null, false), FILE, Constants.LOCK_SLEEP); FileLock lock = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP);
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLock.LOCK_FILE);
lock.unlock(); lock.unlock();
} }
private void testSimple() throws SQLException { private void testSimple() throws SQLException {
FileLock lock1 = new FileLock(new TraceSystem(null, false), FILE, Constants.LOCK_SLEEP); FileLock lock1 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP);
FileLock lock2 = new FileLock(new TraceSystem(null, false), FILE, Constants.LOCK_SLEEP); FileLock lock2 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP);
lock1.lock(FileLock.LOCK_FILE); lock1.lock(FileLock.LOCK_FILE);
try { try {
lock2.lock(FileLock.LOCK_FILE); lock2.lock(FileLock.LOCK_FILE);
...@@ -73,7 +73,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -73,7 +73,7 @@ public class TestFileLock extends TestBase implements Runnable {
// expected // expected
} }
lock1.unlock(); lock1.unlock();
lock2 = new FileLock(new TraceSystem(null, false), FILE, Constants.LOCK_SLEEP); lock2 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP);
lock2.lock(FileLock.LOCK_FILE); lock2.lock(FileLock.LOCK_FILE);
lock2.unlock(); lock2.unlock();
} }
...@@ -101,7 +101,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -101,7 +101,7 @@ public class TestFileLock extends TestBase implements Runnable {
public void run() { public void run() {
FileLock lock = null; FileLock lock = null;
while (!stop) { while (!stop) {
lock = new FileLock(new TraceSystem(null, false), FILE, 100); lock = new FileLock(new TraceSystem(null), FILE, 100);
try { try {
lock.lock(allowSockets ? FileLock.LOCK_SOCKET : FileLock.LOCK_FILE); lock.lock(allowSockets ? FileLock.LOCK_SOCKET : FileLock.LOCK_FILE);
base.trace(lock + " locked"); base.trace(lock + " locked");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论