/**
  * method to close the existing database connections as well as Prepared Statement and ResultSet
  * will be closed.
  *
  * @param pstmt
  * @param rs
  * @param con
  */
 public static void closeDBObjects(PreparedStatement pstmt, ResultSet rs, Connection con) {
   LOG.info("tring to close connection to the db, prepared stmt and result set");
   if (pstmt != null) {
     try {
       LOG.debug("closing prepared statement " + pstmt.toString());
       pstmt.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       LOG.error("pstmt closing error" + e);
       e.printStackTrace();
     }
   }
   if (rs != null) {
     try {
       LOG.debug("closing result set" + rs.toString());
       rs.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       LOG.error("result set closing error" + e);
       e.printStackTrace();
     }
   }
   if (con != null) {
     try {
       LOG.debug("closing database connection" + con.toString());
       con.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       LOG.error("error while closing the connection object of DB" + e);
       e.printStackTrace();
     }
   }
 }
 public DetectConnection(java.sql.Connection inner) {
   this.inner = inner;
   int service = 0;
   long txid = 0;
   TraceContext ctx = TraceContextManager.getLocalContext();
   if (ctx != null) {
     service = ctx.serviceHash;
     txid = ctx.txid;
   }
   if (conf._summary_connection_leak_fullstack_enabled) {
     this.object =
         new LeakableObject(new CONNECTION_NOT_CLOSE(), inner.toString(), service, txid, true);
   } else {
     this.object = new LeakableObject(error, inner.toString(), service, txid, false);
   }
 }
 public String toString() {
   if (conn != null) {
     return conn.toString();
   } else {
     return "closed-conn-" + System.identityHashCode(this);
   }
 }
  public static void main(String[] args) {
    Connection con = null;
    DatabaseMetaData dbmd = null;

    try {
      Class.forName(driver);
      con = DriverManager.getConnection(url);
      System.out.println(con.toString());
      // Use the database connection somehow.

      dbmd = con.getMetaData();

      System.out.println("\n----------------------------------------------------");
      System.out.println("Database Name    = " + dbmd.getDatabaseProductName());
      System.out.println("Database Version = " + dbmd.getDatabaseProductVersion());
      System.out.println("Driver Name      = " + dbmd.getDriverName());
      System.out.println("Driver Version   = " + dbmd.getDriverVersion());
      System.out.println("Database URL     = " + dbmd.getURL());
      System.out.println("----------------------------------------------------");

    } catch (SQLException se) {
      printSQLException(se);
    } catch (ClassNotFoundException e) {
      System.out.println("JDBC Driver " + driver + " not found in CLASSPATH");
    } finally {
      if (con != null) {
        try {
          con.close();
        } catch (SQLException se) {
          printSQLException(se);
        }
      }
    }
  }
 /** This method used to connect to the Conection pool */
 public void connect() throws ConnectException, RemoteException {
   try {
     conn = null;
     conn = dbconn.open();
     USFEnv.getLog().writeDebug("created db connection(), ref: " + conn.toString(), this, null);
   } catch (Exception e) {
     throw new ConnectException("Database connection failure");
   }
 }
Beispiel #6
0
  /** @return conn */
  public Connection getConn() {
    java.sql.Connection conn = null;

    try {
      conn = ((DataSource) dataSources.get(DATA_SOURCE_NAME)).getConnection();
      logger.debug("[CONN ID]=" + conn.toString() + "[ISOPEN]");
    } catch (Exception e) {
      logger.error("ERROR AL OBTENER LA CONEXION" + e);
    }

    return conn;
  }
 public DbConnector() {
   try {
     conn =
         DriverManager.getConnection(
             "jdbc:mysql://localhost/sosecurity?" + "user=ssuser&password=sspwd");
     System.out.println("I am connected to DB " + conn.toString());
   } catch (SQLException ex) {
     // handle any errors
     System.out.println("SQLException: " + ex.getMessage());
     System.out.println("SQLState: " + ex.getSQLState());
     System.out.println("VendorError: " + ex.getErrorCode());
   }
 }
Beispiel #8
0
 /**
  * @param utx
  * @param con
  */
 public void finalizeTransaction(java.sql.Connection localcon) {
   try {
     String idconn = localcon.toString();
     if (isCommit()) {
       utx.commit();
     } else {
       utx.rollback();
     }
     if (localcon != null) {
       localcon.close();
     }
     logger.debug("[CONN ID]=" + idconn + "[ISCLOSED]=" + localcon.isClosed());
   } catch (Exception e) {
     logger.error("[ERROR][finalizeTransaction]:" + e.getMessage());
   }
 }
Beispiel #9
0
  private boolean isValid(final Connection connection) {
    if (connection == null) {
      return false;
    }

    if (status != PwmService.STATUS.OPEN) {
      return false;
    }

    try {
      final Method getFreeSpaceMethod = File.class.getMethod("isValid");
      final Object rawResult = getFreeSpaceMethod.invoke(connection, 10);
      return (Boolean) rawResult;
    } catch (NoSuchMethodException e) {
      /* no error, pre java 1.6 doesn't have this method */
    } catch (Exception e) {
      LOGGER.debug(
          "error checking for isValid for " + connection.toString() + ",: " + e.getMessage());
    }

    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT * FROM ")
        .append(DatabaseTable.PWM_META.toString())
        .append(" WHERE " + KEY_COLUMN + " = ?");
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      statement = connection.prepareStatement(sb.toString());
      statement.setString(1, KEY_ENGINE_START_PREFIX + instanceID);
      statement.setMaxRows(1);
      resultSet = statement.executeQuery();
      if (resultSet.next()) {
        resultSet.getString(VALUE_COLUMN);
      }
    } catch (SQLException e) {
      final ErrorInformation errorInformation =
          new ErrorInformation(
              PwmError.ERROR_DB_UNAVAILABLE, "isValid operation failed: " + e.getMessage());
      lastError = errorInformation;
      LOGGER.error(errorInformation.toDebugStr());
      return false;
    } finally {
      close(statement);
      close(resultSet);
    }
    return true;
  }
Beispiel #10
0
  public static void main(String[] args) {
    try {
      DataAccess da = new DataAccess();
      da.connect();

      Connection conn = da.getConnection();

      trace("Connected to database: " + conn.toString());

      testAddUser(da);
    } catch (AtmDataException ex) {
      trace(ex.getMessage());
      ex.getStackTrace();
    } catch (Exception ex) {
      trace(ex.getMessage());
      ex.getStackTrace();
    }
  }
  /**
   * Business unit of work This method is used to view a particular selected Customer
   *
   * @param customer Id
   * @return customer details
   */
  public RhccCustInfos viewCustomer(String customerId) throws RemoteException {
    RhccCustInfos cTmp = null;

    USFEnv.getLog().writeWarn(" Inside viewCustomer", this, null);

    // Get the connection
    RhccCustInfos rhcc_obj = new RhccCustInfos(conn);
    try {
      if (conn != null) {
        cTmp = rhcc_obj.searchCustomerInfo(customerId);
        USFEnv.getLog()
            .writeDebug("viewCustomer DB connection, ref: " + conn.toString(), this, null);
      }
    } catch (Exception e) {
      USFEnv.getLog().writeCrit("Error in business EJB: ", this, e);
    }
    return cTmp;
  }
  /**
   * Business unit of work This method is used to update an existing customer after editing
   *
   * @param customer details
   * @return true or false whether the customer is updated or not
   */
  public boolean updateCustomer(RhccCustInfos updateCustomer) throws RemoteException {
    USFEnv.getLog().writeWarn(" Inside updateCustomer", this, null);
    boolean bTmp = false;

    // Get the connection
    RhccCustInfos rhcc_obj = new RhccCustInfos(conn);

    try {
      if (conn != null) {
        bTmp = rhcc_obj.updateCustomerInfo(updateCustomer);
        USFEnv.getLog()
            .writeDebug("newCustomer DB connection, ref: " + conn.toString(), this, null);
      }
    } catch (Exception e) {
      USFEnv.getLog().writeCrit("Error in business EJB: ", this, e);
    }
    return bTmp;
  }
  /**
   * Business unit of work This method is used to delete an existing customer
   *
   * @param customer Id
   * @return true or false whether the customer is deleted or not
   */
  public boolean delCustomer(String customerId) throws RemoteException {
    USFEnv.getLog().writeWarn(" Inside delCustomer", this, null);
    boolean bTmp = false;

    // Get the connection
    RhccCustInfos rhcc_obj = new RhccCustInfos(conn);

    try {
      if (conn != null) {
        bTmp = rhcc_obj.deleteCustomer(Long.parseLong(customerId));
        USFEnv.getLog()
            .writeDebug("delCustomer DB connection, ref: " + conn.toString(), this, null);
      }
    } catch (Exception e) {
      USFEnv.getLog().writeCrit("Error in business EJB: ", this, e);
    }
    return bTmp;
  }
Beispiel #14
0
  /** Marks a flag in the ConnectionObject to indicate this connection is no longer in use */
  public synchronized void removeConnection(Connection c) {
    if (c == null) return;

    ConnectionObject co = null;
    for (int i = 0; i < connections.size(); i++) {
      co = (ConnectionObject) connections.elementAt(i);
      if (c == co.connection) {
        try {
          c.close();
          connections.removeElementAt(i);
          trace("Removed " + c.toString());
        } catch (Exception e) {
          e.printStackTrace();
        }

        break;
      }
    }
  }
  /**
   * Business unit of work This method show all the RHCC customers for a funding year
   *
   * @param year - funding year
   * @return vCustnmid - Customers for a Funding Year
   */
  public Vector getAllRHCCCustomers(short year) throws RemoteException, NullPointerException {
    Vector vCustnmid = null;

    USFEnv.getLog().writeWarn("Inside getAllRHCCCustomers", this, null);

    // Get the connection

    RhccCustInfos rhcc_obj = new RhccCustInfos();
    try {

      vCustnmid = rhcc_obj.getAllRHCCCustomers(year);
      USFEnv.getLog()
          .writeDebug("getAllRHCCCustomers DB connection, ref: " + conn.toString(), this, null);

    } catch (Exception e) {
      USFEnv.getLog().writeCrit("Error in business EJB: ", this, e);
    }
    return vCustnmid;
  }
  /**
   * Business unit of work This method is used to save a new Customer or an existing customer after
   * editing
   *
   * @param new customer details
   * @return true or false whether the customer is saved or not
   */
  public boolean saveCustomer(RhccCustInfos saveCustomer, Hashtable custErrors)
      throws RemoteException {
    USFEnv.getLog().writeWarn(" Inside saveCustomer", this, null);

    long customerId;
    boolean bCustErr = false;
    boolean bTmp = false;

    // Get the connection
    RhccCustInfos rhcc_obj = new RhccCustInfos(conn);

    try {
      customerId = saveCustomer.getCustomerId();

      if (conn != null) {
        USFEnv.getLog()
            .writeDebug("saveCustomer DB connection, ref: " + conn.toString(), this, null);
      }
      // Check business logic field value validation
      bCustErr = rhcccustValidate.customerBusVal(saveCustomer, custErrors);

      // Check if critial error occured
      if (!bCustErr) {
        // Saving a new customer
        if (customerId == 0) {
          // One time funding year set for new customer record
          bTmp = newCustomer(saveCustomer);

          USFEnv.getLog()
              .writeWarn(
                  "new RhccCustInfo created, RCI_ID: " + saveCustomer.getCustomerId(), this, null);
        } else {
          // Saving an exiting customer
          bTmp = updateCustomer(saveCustomer);
        }
      }

    } catch (Exception e) {
      USFEnv.getLog().writeCrit("Error in business EJB: ", this, e);
    }
    return bTmp;
  }
  @Override
  public Connection getConnection() {

    Connection con = null;

    try {
      if (conexoesUtilizadas.size() < numeroMaximoConexoes) {
        con = conexoesLivres.poll();
        if (con == null) {
          con = ds.getConnection();
        } else if (con.isClosed()) {
          this.getConnection();
        }
        conexoesUtilizadas.put(con.toString(), con);
      }
    } catch (SQLException e) {
      System.out.println("Problemas com o Poll!" + e.getMessage());
      e.printStackTrace();
    }
    return con;
  }
 public String toString(boolean detailed) {
   if (type == ConnectionType.DIRECT) {
     return String.format("%s(%s) DIRECT TO %s", name, liveness(), ds.toString());
   } else if (type == ConnectionType.CLUSTER) {
     StringBuilder builder = new StringBuilder();
     builder
         .append(name)
         .append('(')
         .append(liveness())
         .append(") VIA CLUSTER TO ")
         .append(connection.toString());
     return builder.toString();
   } else if (type == ConnectionType.CONNECTOR) {
     return String.format("%s(%s) CONNECTOR TO HOST %s", name, liveness(), getContext());
   } else if (type == ConnectionType.BRIDGED) {
     return name + " BRIDGED to " + ds != null ? ds.getName() : "null";
   } else {
     CLUtils.println(String.format("no connection status logic for type %s", type));
     return "UNKNOWN";
   }
 }
 /** {@inheritDoc} */
 @Override
 public String toString() {
   return connection.toString();
 }
 @Override
 public void liberarConnection(Connection con) {
   conexoesLivres.add(con);
   conexoesUtilizadas.remove(con.toString());
 }
Beispiel #21
0
 public static void main(String[] args) {
   Connection conn = getDstConnection(dstHost, dstDBname, dstUser, dstPasswd);
   System.out.println(conn.toString());
 }
Beispiel #22
0
 public String toString() {
   return con.toString();
 }