/** * Automatically re-connect if necessary and if configured to do so. * * @param count the retry count index * @return true if reconnected */ private boolean autoReconnect(int count) { if (!isClosed()) { return false; } if (!autoReconnect) { return false; } if (!cluster && !autoCommit) { return false; } if (count > SysProperties.MAX_RECONNECT) { return false; } lastReconnect++; while (true) { try { embedded = connectEmbeddedOrServer(false); break; } catch (DbException e) { if (e.getErrorCode() != ErrorCode.DATABASE_IS_IN_EXCLUSIVE_MODE) { throw e; } // exclusive mode: re-try endlessly try { Thread.sleep(500); } catch (Exception e2) { // ignore } } } if (embedded == this) { // connected to a server somewhere else embedded = null; } else { // opened an embedded connection now - // must connect to this database in server mode // unfortunately connectEmbeddedOrServer(true); } recreateSessionState(); if (eventListener != null) { eventListener.setProgress( DatabaseEventListener.STATE_RECONNECTED, databaseName, count, SysProperties.MAX_RECONNECT); } return true; }
/** * Open a new (remote or embedded) session. * * @param openNew whether to open a new session in any case * @return the session */ public SessionInterface connectEmbeddedOrServer(boolean openNew) { ConnectionInfo ci = connectionInfo; if (ci.isRemote() || ci.isDynamic()) { connectServer(ci); return this; } // create the session using reflection, // so that the JDBC layer can be compiled without it boolean autoServerMode = Boolean.valueOf(ci.getProperty("AUTO_SERVER", "false")).booleanValue(); ConnectionInfo backup = null; try { if (autoServerMode) { backup = (ConnectionInfo) ci.clone(); connectionInfo = (ConnectionInfo) ci.clone(); } if (openNew) { ci.setProperty("OPEN_NEW", "true"); } if (sessionFactory == null) { sessionFactory = ci.getSessionFactory(); } return sessionFactory.createSession(ci); } catch (Exception re) { DbException e = DbException.convert(re); if (e.getErrorCode() == ErrorCode.DATABASE_ALREADY_OPEN_1) { if (autoServerMode) { String serverKey = ((JdbcSQLException) e.getSQLException()).getSQL(); if (serverKey != null) { backup.setServerKey(serverKey); // OPEN_NEW must be removed now, otherwise // opening a session with AUTO_SERVER fails // if another connection is already open backup.removeProperty("OPEN_NEW", null); connectServer(backup); return this; } } } throw e; } }