/** Creates a Pooled connection and adds it to the connection pool. */
  private void installConnection() throws EmanagerDatabaseException {
    logger.debug("enter");

    PooledConnection connection;

    try {
      connection = poolDataSource.getPooledConnection();
      connection.addConnectionEventListener(this);
      connection.getConnection().setAutoCommit(false);
      synchronized (connectionPool) {
        connectionPool.add(connection);
        logger.debug("Database connection added.");
      }
    } catch (SQLException ex) {
      logger.fatal("exception caught while obtaining database " + "connection: ex = " + ex);
      SQLException ex1 = ex.getNextException();
      while (ex1 != null) {
        logger.fatal("chained sql exception ex1 = " + ex1);
        SQLException nextEx = ex1.getNextException();
        ex1 = nextEx;
      }
      String logString;
      EmanagerDatabaseException ede;

      logString =
          EmanagerDatabaseStatusCode.DatabaseConnectionFailure.getStatusCodeDescription()
              + ex.getMessage();

      logger.fatal(logString);
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.DatabaseConnectionFailure, logString);
      throw ede;
    }
  }
예제 #2
0
 protected boolean insertComment() throws SQLException {
   try {
     String dburl = getServletContext().getInitParameter("DBUrl");
     String dbuser = getServletContext().getInitParameter("DBUser");
     String dbpasswd = getServletContext().getInitParameter("DBPasswd");
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(dburl, dbuser, dbpasswd);
     String sqlqry = "INSERT INTO feedback (name, email, feedback) VALUES (?,?,?)";
     pstmt = conn.prepareStatement(sqlqry);
     pstmt.setString(1, name);
     pstmt.setString(2, email);
     pstmt.setString(3, comment);
     if (pstmt.executeUpdate() > 0) return true;
   } catch (ClassNotFoundException e) {
     // TODO Auto-generated catch block
     logger.error(e);
     e.printStackTrace();
   } catch (SQLException e) {
     // TODO Auto-generated catch block
     logger.error(e);
     e.printStackTrace();
   } finally {
     conn.close();
     pstmt.close();
   }
   return false;
 }
예제 #3
0
  protected void processData(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException {
    try {
      String dispatchUrl = "/feedback.jsp";
      name = request.getParameter("txtName");
      email = request.getParameter("txtEmail");
      comment = request.getParameter("txtComment");

      if (!insertComment()) {
        logger.error("Error. Could not insert comment");
        request.setAttribute("status", "fail");
      } else {
        request.setAttribute("status", "success");
      }
      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(dispatchUrl);
      dispatcher.include(request, response);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      logger.error(e);
      e.printStackTrace();
    } finally {
      conn.close();
      pstmt.close();
    }
  }
예제 #4
0
 /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   // TODO Auto-generated method stub
   try {
     processData(request, response);
   } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
  /**
   * Gets the ConnectionID attribute of the SybaseConnector class
   *
   * @param connection
   * @return The ConnectionID value
   */
  public static long getConnectionID(Connection connection) {
    try {
      Statement stmt = connection.createStatement();
      ResultSet res = stmt.executeQuery("select connection_property('Number')");
      res.next();
      return res.getLong(1);
    } catch (SQLException ex) {
      logger.error("SQL exception retrieving connection ID:" + ex.getMessage());
    }

    return InvalidConnectionId;
  }
  /**
   * @return Connection
   * @roseuid 3F3A5FFD0338
   */
  public Connection getConnection() throws EmanagerDatabaseException {
    long connectionId;
    Connection connection;
    PooledConnection pooledConnection;

    connection = null;
    pooledConnection = null;
    connectionId = InvalidConnectionId;

    try {
      synchronized (connectionPool) {
        if (!connectionPool.isEmpty()) {
          try {
            boolean connectionClosed;

            connectionClosed = false;

            pooledConnection = (PooledConnection) connectionPool.remove(0);
            connection = pooledConnection.getConnection();
            connection.clearWarnings();
            connectionId = getConnectionID(connection);
            connectionClosed = connection.isClosed();
            if (connectionId == InvalidConnectionId || connectionClosed == true) {
              logger.debug("Pooled connection closed.");
              connection = null;
            }
          } catch (SQLException sqe) {
            logger.debug("Pooled connection closed.");
            connection = null;
          }
        }
      }

      if (connection == null) {
        logger.debug("Getting a new connection.");
        pooledConnection = poolDataSource.getPooledConnection();
        pooledConnection.addConnectionEventListener(this);
        connection = pooledConnection.getConnection();
        connection.clearWarnings();
        connectionId = getConnectionID(connection);
      }
    } catch (SQLException sqe) {
      String logString;
      EmanagerDatabaseException ede;

      logString =
          EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription()
              + sqe.getMessage();

      logger.error(logString);
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection, logString);
      throw ede;
    }

    if (connectionId == InvalidConnectionId) {
      EmanagerDatabaseException ede;
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection,
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription());
      throw ede;
    }

    logger.debug(
        "\n*****************************"
            + "\nPooled Connection Init"
            + "\nCon ID:"
            + connectionId
            + "\nCon Object:"
            + pooledConnection
            + "\nPool Object:"
            + connection
            + "\n*****************************");

    return connection;
  }