Beispiel #1
0
 /**
  * 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;
   }
 }