Package com.jolbox.bonecp.hooks

Examples of com.jolbox.bonecp.hooks.ConnectionHook


            bconfig.setCloseConnectionWatchTimeout(10, TimeUnit.MINUTES);
        }
        bconfig.setLazyInit(false);
        bconfig.setDisableJMX(true);
        // Do not manage retry
        ConnectionHook hook = new AbstractConnectionHook() {
            public boolean onAcquireFail(Throwable t, AcquireFailConfig acquireConfig) {
                LOG.error("Failed to obtain connection", t);
                return false;
            }
        };
View Full Code Here


    boolean tryAgain = false;
    Throwable failedThrowable = null;
   
    ConnectionHandle con = this.connectionHandle.get();
    TransactionRecoveryResult recoveryResult = con.recoveryResult;
    ConnectionHook connectionHook = con.getPool().getConfig().getConnectionHook();
   
    int acquireRetryAttempts = con.getPool().getConfig().getAcquireRetryAttempts();
    long acquireRetryDelay = con.getPool().getConfig().getAcquireRetryDelayInMs();
    AcquireFailConfig acquireConfig = new AcquireFailConfig();
    acquireConfig.setAcquireRetryAttempts(new AtomicInteger(acquireRetryAttempts));
    acquireConfig.setAcquireRetryDelayInMs(acquireRetryDelay);
    acquireConfig.setLogMessage("Failed to replay transaction");
   
    Map<Object, Object> replaceTarget = new HashMap<Object, Object>();
    do{
      replaceTarget.clear();
      // make a copy
      for (Entry<Object, Object> entry: recoveryResult.getReplaceTarget().entrySet()){
        replaceTarget.put(entry.getKey(), entry.getValue());
      }

      List<PreparedStatement> prepStatementTarget = new ArrayList<PreparedStatement>();
      List<CallableStatement> callableStatementTarget = new ArrayList<CallableStatement>();
      List<Statement> statementTarget = new ArrayList<Statement>();
      Object result = null;
      tryAgain = false;
      // this connection is dead
      con.setInReplayMode(true); // don't go in a loop of saving our saved log!
      try{
        con.clearStatementCaches(true);
        con.getInternalConnection().close();
      } catch(Throwable t){
        // do nothing - also likely to fail here
      }
      con.setInternalConnection(memorize(con.obtainInternalConnection(), con));
      con.getOriginatingPartition().trackConnectionFinalizer(con); // track this too.

      for (ReplayLog replay: oldReplayLog){

        // we got new connections/statement handles so replace what we've got with the new ones
        if (replay.getTarget() instanceof Connection){
          replaceTarget.put(replay.getTarget(), con.getInternalConnection());
        else if (replay.getTarget() instanceof CallableStatement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), callableStatementTarget.remove(0));
          }
        } else if (replay.getTarget() instanceof PreparedStatement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), prepStatementTarget.remove(0));
          }
        }else if (replay.getTarget() instanceof Statement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), statementTarget.remove(0));
          }
        }


        try {
          // run again using the new connection/statement
          //          result = replay.getMethod().invoke(, replay.getArgs());
          result = runWithPossibleProxySwap(replay.getMethod(), replaceTarget.get(replay.getTarget()), replay.getArgs());

          // remember what we've got last
          recoveryResult.setResult(result);

          // if we got a new statement (eg a prepareStatement call), save it, we'll use it for our search/replace
          if (result instanceof CallableStatement){
            callableStatementTarget.add((CallableStatement)result);
          } else if (result instanceof PreparedStatement){
            prepStatementTarget.add((PreparedStatement)result);
          } else if (result instanceof Statement){
            statementTarget.add((Statement)result);
         
        } catch (Throwable t) {
          // It blew up again, let's try a couple more times before giving up...
          // call the hook, if available.
          if (connectionHook != null){
            tryAgain = connectionHook.onAcquireFail(t, acquireConfig);
          } else {

            logger.error("Failed to replay transaction. Sleeping for "+acquireRetryDelay+"ms and trying again. Attempts left: "+acquireRetryAttempts+". Exception: "+t.getCause());

            try {
View Full Code Here

    ConnectionHandle con = this.connectionHandle.get();
    if (con == null){ // safety!
      throw PoolUtil.generateSQLException("ConnectionHandle is gone!", new IllegalStateException());
    }
    TransactionRecoveryResult recoveryResult = con.recoveryResult;
    ConnectionHook connectionHook = con.getPool().getConfig().getConnectionHook();

    int acquireRetryAttempts = con.getPool().getConfig().getAcquireRetryAttempts();
    long acquireRetryDelay = con.getPool().getConfig().getAcquireRetryDelayInMs();
    AcquireFailConfig acquireConfig = new AcquireFailConfig();
    acquireConfig.setAcquireRetryAttempts(new AtomicInteger(acquireRetryAttempts));
    acquireConfig.setAcquireRetryDelayInMs(acquireRetryDelay);
    acquireConfig.setLogMessage("Failed to replay transaction");

    Map<Object, Object> replaceTarget = new HashMap<Object, Object>();
    do{
      replaceTarget.clear();
      // make a copy
      for (Entry<Object, Object> entry: recoveryResult.getReplaceTarget().entrySet()){
        replaceTarget.put(entry.getKey(), entry.getValue());
      }

      List<PreparedStatement> prepStatementTarget = new ArrayList<PreparedStatement>();
      List<CallableStatement> callableStatementTarget = new ArrayList<CallableStatement>();
      List<Statement> statementTarget = new ArrayList<Statement>();
      Object result = null;
      tryAgain = false;
      // this connection is dead
      con.setInReplayMode(true); // don't go in a loop of saving our saved log!
      try{
        con.clearStatementCaches(true);
        con.getInternalConnection().close();
      } catch(Throwable t){
        // do nothing - also likely to fail here
      }
      try{
        con.setInternalConnection(memorize(con.getPool().obtainInternalConnection(con), con));
      } catch(SQLException e){
        throw con.markPossiblyBroken(e);
      }

      con.getOriginatingPartition().trackConnectionFinalizer(con); // track this too.

      for (ReplayLog replay: oldReplayLog){

        // we got new connections/statement handles so replace what we've got with the new ones
        if (replay.getTarget() instanceof Connection){
          replaceTarget.put(replay.getTarget(), con.getInternalConnection());
        else if (replay.getTarget() instanceof CallableStatement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), callableStatementTarget.remove(0));
          }
        } else if (replay.getTarget() instanceof PreparedStatement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), prepStatementTarget.remove(0));
          }
        }else if (replay.getTarget() instanceof Statement){
          if (replaceTarget.get(replay.getTarget()) == null){
            replaceTarget.put(replay.getTarget(), statementTarget.remove(0));
          }
        }


        try {
          // run again using the new connection/statement
          //          result = replay.getMethod().invoke(, replay.getArgs());
          result = runWithPossibleProxySwap(replay.getMethod(), replaceTarget.get(replay.getTarget()), replay.getArgs());

          // remember what we've got last
          recoveryResult.setResult(result);

          // if we got a new statement (eg a prepareStatement call), save it, we'll use it for our search/replace
          if (result instanceof CallableStatement){
            callableStatementTarget.add((CallableStatement)result);
          } else if (result instanceof PreparedStatement){
            prepStatementTarget.add((PreparedStatement)result);
          } else if (result instanceof Statement){
            statementTarget.add((Statement)result);
         
        } catch (Throwable t) {
          // It blew up again, let's try a couple more times before giving up...
          // call the hook, if available.
          if (connectionHook != null){
            tryAgain = connectionHook.onAcquireFail(t, acquireConfig);
          } else {

            logger.error("Failed to replay transaction. Sleeping for "+acquireRetryDelay+"ms and trying again. Attempts left: "+acquireRetryAttempts+". Exception: "+t.getCause() + " Message:"+t.getMessage());

            try {
View Full Code Here

    expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes();
    mockPartition.updateCreatedConnections(-1);
    expectLastCall().once();
    mockPartition.setUnableToCreateMoreTransactions(false);
    expectLastCall().once();
    ConnectionHook mockConnectionHook = EasyMock.createNiceMock(ConnectionHook.class);
    expect(mockConnection.getConnectionHook()).andReturn(mockConnectionHook).anyTimes();
    expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes();

    mockConnectionHook.onDestroy(mockConnection);
    expectLastCall().once();
    replay(mockRealConnection, mockConnectionHook, mockConnection);
    testClass.postDestroyConnection(mockConnection);
    verify(mockConnectionHook, mockConnection);
  }
View Full Code Here

    long acquireRetryDelayInMs = this.getConfig().getAcquireRetryDelayInMs();
    AcquireFailConfig acquireConfig = new AcquireFailConfig();
    acquireConfig.setAcquireRetryAttempts(new AtomicInteger(acquireRetryAttempts));
    acquireConfig.setAcquireRetryDelayInMs(acquireRetryDelayInMs);
    acquireConfig.setLogMessage("Failed to acquire connection to "+url);
    ConnectionHook connectionHook = this.getConfig().getConnectionHook();
    do{
      result = null;
      try {
        // keep track of this hook.
        result = this.obtainRawInternalConnection();
        tryAgain = false;

        if (acquireRetryAttempts != this.getConfig().getAcquireRetryAttempts()){
          logger.info("Successfully re-established connection to "+url);
        }
       
        this.getDbIsDown().set(false);
       
        connectionHandle.setInternalConnection(result);
       
        // call the hook, if available.
        if (connectionHook != null){
          connectionHook.onAcquire(connectionHandle);
        }

       
        ConnectionHandle.sendInitSQL(result, this.getConfig().getInitSQL());
      } catch (SQLException e) {
        // call the hook, if available.
        if (connectionHook != null){
          tryAgain = connectionHook.onAcquireFail(e, acquireConfig);
        } else {
          logger.error(String.format("Failed to acquire connection to %s. Sleeping for %d ms. Attempts left: %d", url, acquireRetryDelayInMs, acquireRetryAttempts), e);

          try {
            if (acquireRetryAttempts > 0){
View Full Code Here

   
    assertEquals(60000, config.getAcquireRetryDelay());
    assertEquals("foo", config.getDefaultTransactionIsolation());
    assertEquals(123, config.getDefaultTransactionIsolationValue());
   
    ConnectionHook hook = new AbstractConnectionHook() {
      // do nothing
    };
    config.setConnectionHook(hook);
   
    config.setStatementsCachedPerConnection(7);
View Full Code Here

TOP

Related Classes of com.jolbox.bonecp.hooks.ConnectionHook

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.