Solved

Disconnecting from DB after getting data

Posted on 2010-11-09
10
349 Views
Last Modified: 2012-05-10
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
Comment
Question by:gbcbr
  • 6
  • 3
10 Comments
 
LVL 2

Expert Comment

by:Pacane
Comment Utility
Isn't there a method called .close() on your connection object?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to call ConnectDB.destroy at the end of SelectData.run in a finally block
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>public void destroy(Connection conn) {

The parameter is redundant and you should get rid of it
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Or: leave things as they are and uncomment the call to destroy in ConnectDB.select, which you've commented out
0
 

Author Comment

by:gbcbr
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Forget my last comment: it won't work
0
 

Author Comment

by:gbcbr
Comment Utility
I know, because I try to put this destroy in all places of select, but it doesn't accept anywhere.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
Try the attached
src.zip
0
 

Author Closing Comment

by:gbcbr
Comment Utility
Perfect! Thank you very much for the help!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to recover a database from a user managed backup
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now