示例#1
0
 /**
  * Closes this connection. All open statements, prepared statements and result sets that where
  * created by this connection become invalid after calling this method. If there is an uncommitted
  * transaction, it will be rolled back.
  */
 public synchronized void close() throws SQLException {
   try {
     debugCodeCall("close");
     openStackTrace = null;
     if (executingStatement != null) {
       executingStatement.cancel();
     }
     if (session == null) {
       return;
     }
     session.cancel();
     try {
       synchronized (session) {
         if (!session.isClosed()) {
           try {
             // roll back unless that would require to re-connect
             // (the transaction can't be rolled back after re-connecting)
             if (!session.isReconnectNeeded(true)) {
               rollbackInternal();
               session.afterWriting();
             }
             closePreparedCommands();
           } finally {
             session.close();
           }
         }
       }
     } finally {
       session = null;
     }
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
示例#2
0
 /**
  * INTERNAL. Check if this connection is closed.
  *
  * @param write if the next operation is possibly writing
  * @throws SQLException if the connection or session is closed
  */
 protected void checkClosed(boolean write) throws SQLException {
   if (session == null) {
     throw DbException.get(ErrorCode.OBJECT_CLOSED);
   }
   if (session.isClosed()) {
     throw DbException.get(ErrorCode.DATABASE_CALLED_AT_SHUTDOWN);
   }
   if (session.isReconnectNeeded(write)) {
     trace.debug("reconnect");
     closePreparedCommands();
     session = session.reconnect(write);
     setTrace(session.getTrace());
   }
 }
示例#3
0
 /** INTERNAL */
 public JdbcConnection(ConnectionInfo ci, boolean useBaseDir) throws SQLException {
   try {
     if (useBaseDir) {
       String baseDir = SysProperties.getBaseDir();
       if (baseDir != null) {
         ci.setBaseDir(baseDir);
       }
     }
     checkJavaVersion();
     // this will return an embedded or server connection
     session = new SessionRemote().createSession(ci);
     trace = session.getTrace();
     int id = getNextId(TraceObject.CONNECTION);
     setTrace(trace, TraceObject.CONNECTION, id);
     this.user = ci.getUserName();
     if (isInfoEnabled()) {
       trace.infoCode(
           "Connection "
               + getTraceObjectName()
               + " = DriverManager.getConnection("
               + quote(ci.getOriginalURL())
               + ", "
               + quote(user)
               + ", \"\");");
     }
     this.url = ci.getURL();
     openStackTrace = new Exception("Stack Trace");
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
示例#4
0
 /**
  * Returns true if this connection has been closed.
  *
  * @return true if close was called
  */
 public boolean isClosed() throws SQLException {
   try {
     debugCodeCall("isClosed");
     return session == null || session.isClosed();
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
示例#5
0
 /** INTERNAL */
 public JdbcConnection(JdbcConnection clone) {
   this.session = clone.session;
   trace = session.getTrace();
   int id = getNextId(TraceObject.CONNECTION);
   setTrace(trace, TraceObject.CONNECTION, id);
   this.user = clone.user;
   this.url = clone.url;
 }
示例#6
0
 /** INTERNAL */
 public JdbcConnection(SessionInterface session, String user, String url) {
   isInternal = true;
   this.session = session;
   trace = session.getTrace();
   int id = getNextId(TraceObject.CONNECTION);
   setTrace(trace, TraceObject.CONNECTION, id);
   this.user = user;
   this.url = url;
 }
示例#7
0
 /**
  * Create a Blob value from this input stream.
  *
  * @param x the input stream
  * @param length the length (if smaller or equal than 0, all data until the end of file is read)
  * @return the value
  */
 public Value createBlob(InputStream x, long length) {
   if (x == null) {
     return ValueNull.INSTANCE;
   }
   if (length <= 0) {
     length = -1;
   }
   Value v = session.getDataHandler().getLobStorage().createBlob(x, length);
   return v;
 }
示例#8
0
 /**
  * INTERNAL. Check if the statement is closed.
  *
  * @param write if the next operation is possibly writing
  * @return true if a reconnect was required
  * @throws SQLException if it is closed
  */
 protected boolean checkClosed(boolean write) throws SQLException {
   if (conn == null) {
     throw Message.getSQLException(ErrorCode.OBJECT_CLOSED);
   }
   conn.checkClosed(write);
   SessionInterface s = conn.getSession();
   if (s != session) {
     session = s;
     setTrace(session.getTrace());
     return true;
   }
   return false;
 }
示例#9
0
 JdbcStatement(
     JdbcConnection conn,
     int id,
     int resultSetType,
     int resultSetConcurrency,
     boolean closeWithResultSet) {
   this.conn = conn;
   this.session = conn.getSession();
   setTrace(session.getTrace(), TraceObject.STATEMENT, id);
   this.resultSetType = resultSetType;
   this.resultSetConcurrency = resultSetConcurrency;
   this.closedByResultSet = closeWithResultSet;
 }
示例#10
0
 /**
  * Returns true if this connection is still valid.
  *
  * @param timeout the number of seconds to wait for the database to respond (ignored)
  * @return true if the connection is valid.
  */
 public synchronized boolean isValid(int timeout) {
   try {
     debugCodeCall("isValid", timeout);
     if (session == null || session.isClosed()) {
       return false;
     }
     // force a network round trip (if networked)
     getInternalAutoCommit();
     return true;
   } catch (Exception e) {
     // this method doesn't throw an exception, but it logs it
     logAndConvert(e);
     return false;
   }
 }
示例#11
0
 // ## Java 1.6 begin ##
 public NClob createNClob() throws SQLException {
   try {
     int id = getNextId(TraceObject.CLOB);
     debugCodeAssign("NClob", TraceObject.CLOB, id, "createNClob()");
     checkClosedForWrite();
     try {
       Value v =
           session
               .getDataHandler()
               .getLobStorage()
               .createClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0);
       return new JdbcClob(this, v, id);
     } finally {
       afterWriting();
     }
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
示例#12
0
 /** INTERNAL */
 public void setPowerOffCount(int count) {
   if (session != null) {
     session.setPowerOffCount(count);
   }
 }
示例#13
0
 /** INTERNAL */
 public int getPowerOffCount() {
   return (session == null || session.isClosed()) ? 0 : session.getPowerOffCount();
 }
示例#14
0
 /** INTERNAL. Called after executing a command that could have written something. */
 protected void afterWriting() {
   session.afterWriting();
 }
示例#15
0
 private CommandInterface prepareCommand(String sql, CommandInterface old) {
   return old == null ? session.prepareCommand(sql, Integer.MAX_VALUE) : old;
 }
示例#16
0
 /**
  * Prepare an command. This will parse the SQL statement.
  *
  * @param sql the SQL statement
  * @param fetchSize the fetch size (used in remote connections)
  * @return the command
  */
 CommandInterface prepareCommand(String sql, int fetchSize) {
   return session.prepareCommand(sql, fetchSize);
 }