use of org.h2.message.TraceSystem in project h2database by h2database.
the class Database method setBackgroundException.
public void setBackgroundException(DbException e) {
if (backgroundException == null) {
backgroundException = e;
TraceSystem t = getTraceSystem();
if (t != null) {
t.getTrace(Trace.DATABASE).error(e, "flush");
}
}
}
use of org.h2.message.TraceSystem in project h2database by h2database.
the class SessionRemote method connectServer.
private void connectServer(ConnectionInfo ci) {
String name = ci.getName();
if (name.startsWith("//")) {
name = name.substring("//".length());
}
int idx = name.indexOf('/');
if (idx < 0) {
throw ci.getFormatException();
}
databaseName = name.substring(idx + 1);
String server = name.substring(0, idx);
traceSystem = new TraceSystem(null);
String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null);
if (traceLevelFile != null) {
int level = Integer.parseInt(traceLevelFile);
String prefix = getFilePrefix(SysProperties.CLIENT_TRACE_DIRECTORY);
try {
traceSystem.setLevelFile(level);
if (level > 0 && level < 4) {
String file = FileUtils.createTempFile(prefix, Constants.SUFFIX_TRACE_FILE, false, false);
traceSystem.setFileName(file);
}
} catch (IOException e) {
throw DbException.convertIOException(e, prefix);
}
}
String traceLevelSystemOut = ci.getProperty(SetTypes.TRACE_LEVEL_SYSTEM_OUT, null);
if (traceLevelSystemOut != null) {
int level = Integer.parseInt(traceLevelSystemOut);
traceSystem.setLevelSystemOut(level);
}
trace = traceSystem.getTrace(Trace.JDBC);
String serverList = null;
if (server.indexOf(',') >= 0) {
serverList = StringUtils.quoteStringSQL(server);
ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED);
}
autoReconnect = ci.getProperty("AUTO_RECONNECT", false);
// AUTO_SERVER implies AUTO_RECONNECT
boolean autoServer = ci.getProperty("AUTO_SERVER", false);
if (autoServer && serverList != null) {
throw DbException.getUnsupportedException("autoServer && serverList != null");
}
autoReconnect |= autoServer;
if (autoReconnect) {
String className = ci.getProperty("DATABASE_EVENT_LISTENER");
if (className != null) {
className = StringUtils.trim(className, true, true, "'");
try {
eventListener = (DatabaseEventListener) JdbcUtils.loadUserClass(className).newInstance();
} catch (Throwable e) {
throw DbException.convert(e);
}
}
}
cipher = ci.getProperty("CIPHER");
if (cipher != null) {
fileEncryptionKey = MathUtils.secureRandomBytes(32);
}
String[] servers = StringUtils.arraySplit(server, ',', true);
int len = servers.length;
transferList.clear();
sessionId = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
// TODO cluster: support more than 2 connections
boolean switchOffCluster = false;
try {
for (String s : servers) {
try {
Transfer trans = initTransfer(ci, databaseName, s);
transferList.add(trans);
} catch (IOException e) {
if (len == 1) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, e, e + ": " + s);
}
switchOffCluster = true;
}
}
checkClosed();
if (switchOffCluster) {
switchOffCluster();
}
checkClusterDisableAutoCommit(serverList);
} catch (DbException e) {
traceSystem.close();
throw e;
}
}
use of org.h2.message.TraceSystem in project h2database by h2database.
the class FileLister method tryUnlockDatabase.
/**
* Try to lock the database, and then unlock it. If this worked, the
* .lock.db file will be removed.
*
* @param files the database files to check
* @param message the text to include in the error message
* @throws SQLException if it failed
*/
public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
for (String fileName : files) {
if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
try {
lock.lock(FileLockMethod.FILE);
lock.unlock();
} catch (DbException e) {
throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, message).getSQLException();
}
} else if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
try (FileChannel f = FilePath.get(fileName).open("r")) {
java.nio.channels.FileLock lock = f.tryLock(0, Long.MAX_VALUE, true);
lock.release();
} catch (Exception e) {
throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, e, message).getSQLException();
}
}
}
}
use of org.h2.message.TraceSystem in project h2database by h2database.
the class TestTraceSystem method testAdapter.
private void testAdapter() {
TraceSystem ts = new TraceSystem(null);
ts.setName("test");
ts.setLevelFile(TraceSystem.ADAPTER);
ts.getTrace("test").debug("test");
ts.getTrace("test").info("test");
ts.getTrace("test").error(new Exception(), "test");
// The used SLF4J-nop logger has all log levels disabled,
// so this should be reflected in the trace system.
assertFalse(ts.isEnabled(TraceSystem.INFO));
assertFalse(ts.getTrace("test").isInfoEnabled());
ts.close();
}
use of org.h2.message.TraceSystem in project h2database by h2database.
the class TestFileLock method testFutureModificationDate.
private void testFutureModificationDate() throws Exception {
File f = new File(getFile());
f.delete();
f.createNewFile();
f.setLastModified(System.currentTimeMillis() + 10000);
FileLock lock = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock.lock(FileLockMethod.FILE);
lock.unlock();
}
Aggregations