• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Disconnecting from DB after getting data

There are two classes:
 
public class ConnectDB {

    private static String databaseurl = "jdbc:oracle:thin:@10.1.1.7:1521:orcl";
    private static String user = "LIONFX";
    private static String password = "";
    private static String driverName = "oracle.jdbc.OracleDriver";
    
    Connection conn;
    ResultSet rs;
    Statement statement;
    PreparedStatement ps;

    public void insert(String sql) throws SQLException {
        try {
            Class.forName(driverName).newInstance();
            conn = (Connection) DriverManager.getConnection(databaseurl, user, password);
            statement = (Statement) conn.createStatement();
            int st = statement.executeUpdate(sql);
            destroy(conn);
            statement.close();

        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (ClassNotFoundException e) {
        }
    }

    public ResultSet select(String sql) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class.forName(driverName).newInstance();
        conn = (Connection) DriverManager.getConnection(databaseurl, user, password);
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
//        destroy(conn);
        return rs;
    }

    public void destroy(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
        }
    }

Open in new window

which connecting to DB
and SelectData which extract data from this DB:
 
public class SelectData extends TimerTask {

      private ConnectDB conn = new ConnectDB();
      String symbol[] = new String[3];
      double openBid[] = new double[3];
      double openAsk[] = new double[3];
      Timestamp timest[] = new Timestamp[3];

      public void run() {
            try {

      ResultSet rs = conn.select("SELECT * FROM EURUSD_1C where timest = ( select max( timest ) from EURUSD_1C ) and rownum = 1 union all "
                                          + "SELECT * FROM EURJPY_1C where timest = ( select max( timest ) from EURJPY_1C ) and rownum = 1  union all "
                                          + "SELECT * FROM USDJPY_1C where timest = ( select max( timest ) from USDJPY_1C ) and rownum = 1");

                  while (rs.next()) {

                        if (rs.getString("SYMBOL").equals("EUR/USD")) {
                              symbol[0] = rs.getString("SYMBOL");
                              timest[0] = rs.getTimestamp("TIMEST");
                              openBid[0] = rs.getDouble("BIDPX");
                              openAsk[0] = rs.getDouble("ASKPX");

                        } else if (rs.getString("SYMBOL").equals("EUR/JPY")) {
                              symbol[1] = rs.getString("SYMBOL");
                              timest[1] = rs.getTimestamp("TIMEST");
                              openBid[1] = rs.getDouble("BIDPX");
                              openAsk[1] = rs.getDouble("ASKPX");

                        } else if (rs.getString("SYMBOL").equals("USD/JPY")) {
                              symbol[2] = rs.getString("SYMBOL");
                              timest[2] = rs.getTimestamp("TIMEST");
                              openBid[2] = rs.getDouble("BIDPX");
                              openAsk[2] = rs.getDouble("ASKPX");

                        }

                        else {
                              System.out.println("No new rows");
                        }
                  }
                  rs.close();
            } catch (Exception ex) {
                  ex.printStackTrace();
            }
      }
}

Open in new window

For insert method it close sessions well.
The problem is that when ConnectDB connecting to Oracle and get data it doesn't close sessions, and for next query open new session, so in 3 hours it's open all 300 sessions and stack Oracle.
I try to call destroy(conn); like in insert method but when I put it above return rs; I have error - Closed connection.
When I put it after - unreachable statement.
Please advice how to close connection for select method after each session.
0
gbcbr
Asked:
gbcbr
  • 6
  • 3
1 Solution
 
PacaneCommented:
Isn't there a method called .close() on your connection object?
0
 
CEHJCommented:
You need to call ConnectDB.destroy at the end of SelectData.run in a finally block
0
 
CEHJCommented:
>>public void destroy(Connection conn) {

The parameter is redundant and you should get rid of it
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Or: leave things as they are and uncomment the call to destroy in ConnectDB.select, which you've commented out
0
 
gbcbrAuthor Commented:
Please give me more details where to place conn.destroy(conn); into Select Data
because it doesn't accept this statement.


                        } else if (rs.getString("SYMBOL").equals("USD/JPY")) {
                              symbol[2] = rs.getString("SYMBOL");
                              timest[2] = rs.getTimestamp("TIMEST");
                              openBid[2] = rs.getDouble("BIDPX");
                              openAsk[2] = rs.getDouble("ASKPX");

                              conn.destroy((Connection) conn);

java.lang.ClassCastException: lionfx.dbfx.db.ConnectDB cannot be cast to java.sql.Connection
        at lionfx.dbfx.db.SelectData.run(SelectData.java:47)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)
0
 
CEHJCommented:
Forget my last comment: it won't work
0
 
gbcbrAuthor Commented:
I know, because I try to put this destroy in all places of select, but it doesn't accept anywhere.
0
 
CEHJCommented:
Try the attached
src.zip
0
 
gbcbrAuthor Commented:
Perfect! Thank you very much for the help!
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now