/**
  * INTERNAL: Clears customization from connection. Called only if connection is customized
  * (isActive()==true). If the method fails due to SQLException it should "eat" it (just like
  * DatasourceAccessor.closeConnection method). isActive method called after this method should
  * always return false.
  */
 public void clear() {
   try {
     clearConnectionCache();
     if (this.session.shouldLog(SessionLog.FINEST, SessionLog.CONNECTION)) {
       Properties logProperties = proxyProperties;
       if (proxyProperties.containsKey(OracleConnection.PROXY_USER_PASSWORD)) {
         logProperties = (Properties) proxyProperties.clone();
         logProperties.setProperty(OracleConnection.PROXY_USER_PASSWORD, "******");
       }
       Object[] args = new Object[] {oracleConnection, logProperties};
       ((AbstractSession) this.session)
           .log(
               SessionLog.FINEST,
               SessionLog.CONNECTION,
               "proxy_connection_customizer_closing_proxy_session",
               args);
     }
     oracleConnection.close(OracleConnection.PROXY_SESSION);
   } catch (SQLException exception) {
     // Ignore
     this.session
         .getSessionLog()
         .logThrowable(SessionLog.WARNING, SessionLog.CONNECTION, exception);
   } finally {
     oracleConnection = null;
   }
 }
Exemplo n.º 2
0
  public static void main(String args[]) throws SQLException {
    String url = "jdbc:oracle:oci8:@";
    try {
      String url1 = System.getProperty("JDBC_URL");
      if (url1 != null) url = url1;
    } catch (Exception e) {
      // If there is any security exception, ignore it
      // and use the default
    }

    // Create a OracleDataSource instance and set properties
    OracleDataSource ods = new OracleDataSource();
    ods.setUser("hr");
    ods.setPassword("hr");
    ods.setURL(url);

    // Connect to the database
    Connection conn = ods.getConnection();

    Statement stmt = conn.createStatement();
    stmt.execute("delete from departments where department_id > 2000");

    // Default batch value set to 50 for all prepared statements
    // belonging to this connection.
    ((OracleConnection) conn).setDefaultExecuteBatch(50);

    PreparedStatement ps = conn.prepareStatement("insert into departments values (?, ?, ?, ?)");

    ps.setInt(1, 2010);
    ps.setString(2, "Import");
    ps.setInt(3, 114);
    ps.setInt(4, 1700);

    // this execute does not actually happen at this point
    System.out.println(ps.executeUpdate());

    ps.setInt(1, 2020);
    ps.setString(2, "Export");
    ps.setInt(3, 145);
    ps.setInt(4, 2500);

    // this execute does not actually happen at this point
    int rows = ps.executeUpdate();

    System.out.println("Number of rows updated before calling sendBatch: " + rows);

    // Execution of both previously batched executes will happen
    // at this point. The number of rows updated will be
    // returned by sendBatch.
    rows = ((OraclePreparedStatement) ps).sendBatch();

    System.out.println("Number of rows updated by calling sendBatch: " + rows);

    ps.close();
    conn.close();
  }
 /** INTERNAL: Clears both implicit and explicit caches of OracleConnection */
 public void clearOracleConnectionCache(Connection conn) {
   if (conn instanceof OracleConnection) {
     OracleConnection oracleConnection = (OracleConnection) conn;
     try {
       if (oracleConnection.getImplicitCachingEnabled()) {
         oracleConnection.purgeImplicitCache();
       }
     } catch (SQLException ex) {
       // ignore
     }
     try {
       if (oracleConnection.getExplicitCachingEnabled()) {
         oracleConnection.purgeExplicitCache();
       }
     } catch (SQLException ex) {
       // ignore
     }
   }
 }
Exemplo n.º 4
0
 public static boolean getImplicitCachingEnabled(Connection conn) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   return oracleConn.getImplicitCachingEnabled();
 }
Exemplo n.º 5
0
 public static int getDefaultRowPrefetch(Connection conn, int value) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   return oracleConn.getDefaultRowPrefetch();
 }
Exemplo n.º 6
0
 public static void setDefaultRowPrefetch(Connection conn, int value) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   oracleConn.setDefaultRowPrefetch(value);
 }
Exemplo n.º 7
0
 public static int getDefaultExecuteBatch(Connection conn) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   return oracleConn.getDefaultExecuteBatch();
 }
Exemplo n.º 8
0
 public static void openProxySession(Connection conn, int type, java.util.Properties prop)
     throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   oracleConn.openProxySession(type, prop);
 }
Exemplo n.º 9
0
 public static int pingDatabase(Connection conn) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   return oracleConn.pingDatabase(1000);
 }
Exemplo n.º 10
0
 public static void setStatementCacheSize(Connection conn, int size) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   oracleConn.setStatementCacheSize(size);
 }
Exemplo n.º 11
0
 public static void setImplicitCachingEnabled(Connection conn, boolean cache) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   oracleConn.setImplicitCachingEnabled(cache);
 }
Exemplo n.º 12
0
 public static void purgeImplicitCache(Connection conn) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   oracleConn.purgeImplicitCache();
 }
Exemplo n.º 13
0
 public static int getStatementCacheSize(Connection conn) throws SQLException {
   OracleConnection oracleConn = unwrap(conn);
   return oracleConn.getStatementCacheSize();
 }
  /**
   * INTERNAL: Applies customization to connection. Called only if connection is not already
   * customized (isActive()==false). The method may throw SQLException wrapped into
   * DatabaseException. isActive method called after this method should return true only in case the
   * connection was actually customized.
   */
  public void customize() {
    // Lazily initialize proxy properties - customize method may be never called
    // in case of ClientSession using external connection pooling.
    if (proxyProperties == null) {
      buildProxyProperties();
    }

    Connection connection = accessor.getConnection();
    if (connection instanceof OracleConnection) {
      oracleConnection = (OracleConnection) connection;
    } else {
      connection = session.getServerPlatform().unwrapConnection(connection);
      if (connection instanceof OracleConnection) {
        oracleConnection = (OracleConnection) connection;
      } else {
        throw ValidationException.oracleJDBC10_1_0_2ProxyConnectorRequiresOracleConnection();
      }
    }
    try {
      clearConnectionCache();
      Object[] args = null;
      if (this.session.shouldLog(SessionLog.FINEST, SessionLog.CONNECTION)) {
        Properties logProperties = proxyProperties;
        if (proxyProperties.containsKey(OracleConnection.PROXY_USER_PASSWORD)) {
          logProperties = (Properties) proxyProperties.clone();
          logProperties.setProperty(OracleConnection.PROXY_USER_PASSWORD, "******");
        }
        args = new Object[] {oracleConnection, logProperties};
      }
      if (oracleConnection.isProxySession()) {
        // Unexpectedly oracleConnection already has a proxy session - probably it was not closed
        // when connection was returned back to connection pool.
        // That may happen on jta transaction rollback (especially triggered outside of user's
        // thread - such as timeout)
        // when beforeCompletion is never issued
        // and application server neither closes proxySession nor allows access to connection in
        // afterCompletion.
        try {
          if (args != null) {
            ((AbstractSession) this.session)
                .log(
                    SessionLog.FINEST,
                    SessionLog.CONNECTION,
                    "proxy_connection_customizer_already_proxy_session",
                    args);
          }
          oracleConnection.close(OracleConnection.PROXY_SESSION);
        } catch (SQLException exception) {
          // Ignore
          this.session
              .getSessionLog()
              .logThrowable(SessionLog.WARNING, SessionLog.CONNECTION, exception);
        }
      }
      oracleConnection.openProxySession(proxyType, proxyProperties);
      // 12c driver will default to an autoCommit setting of true on calling openProxySession
      oracleConnection.setAutoCommit(false);
      if (args != null) {
        ((AbstractSession) this.session)
            .log(
                SessionLog.FINEST,
                SessionLog.CONNECTION,
                "proxy_connection_customizer_opened_proxy_session",
                args);
      }
    } catch (SQLException exception) {
      oracleConnection = null;
      throw DatabaseException.sqlException(exception);
    } catch (NoSuchMethodError noSuchMethodError) {
      oracleConnection = null;
      throw ValidationException.oracleJDBC10_1_0_2ProxyConnectorRequiresOracleConnectionVersion();
    }
  }