Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Oracle cursor will not close

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Oracle cursor will not close

    Hi,

    I read some data from an Oracle database using a package with a number of functions and refcursors to produce the result sets. The database is read periodicaly via a timer and presented on screen.

    The problem is that regardless to if I close the resultset or not, the database cursor ramains and increases for every refresh of the screen. I have tried diffrent tips that i Googled bot nothing works. I hope someone can see if there is something wrong in the code or show me some kind of solution.

    public class PanelSQL {
    	Tools debug = new Tools();
     
    	// DB info
    	public JPanel DbInfo(Connection conn) throws SQLException {
     
    		// Database info panel
    		JPanel panel = getPanel(" Database ");
    		panel.setLayout(new MigLayout("wrap 4","[] 16 []"));
     
    		// Prepare statement
    		String query = "begin ? := cmt.db_info; end;";
    		ResultSet rs = prepSql(conn, query);
     
    		try {
    			rs.next();
    			panel.add(new JLabel("Database/instance name:"),"left");
    			panel.add(new JTextField(rs.getString(1)+"/"+rs.getString(2)),"grow");
    			panel.add(new JLabel("Version:"),"left");
    			panel.add(new JTextField(rs.getString(3)),"grow");
    			panel.add(new JLabel("Database role:"),"left");
    			panel.add(new JTextField(rs.getString(4)),"grow");
    			panel.add(new JLabel("Instance role:"),"left");
    			panel.add(new JTextField(rs.getString(5)),"grow");
    			panel.add(new JLabel("Open mode:"),"left");
    			panel.add(new JTextField(rs.getString(6)),"grow");
    			panel.add(new JLabel("Log mode:"),"left");
    			panel.add(new JTextField(rs.getString(7)),"grow");
    			panel.add(new JLabel("Status:"),"left");
    			panel.add(new JTextField(rs.getString(8)),"grow");
    			panel.add(new JLabel("Database logins:"),"left");
    			panel.add(new JTextField(rs.getString(9)),"grow");
    			panel.add(new JLabel("Startup time:"),"left");
    			panel.add(new JTextField(rs.getString(10)),"grow");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			try { rs.close(); 
    			} catch (Exception f) {
    				f.printStackTrace();
    			}
    		}
     
    		return panel;
    	}

    Cheers,
    Magnus


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Oracle cursor will not close

    Out of curiosity, is there any difference if you also close (and then reopen for the next transaction) the Connection as well?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Oracle cursor will not close

    Since I'm a newbie at Java I'm not shure. I read somewhere that it can make a difference to close the statement first and then the resultset. I tried to use something like...

    conn (connection passed in to method as parameter)
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement(// Some SQL //);
        rs = stmt.executeQuery();
    } catch {
        // Some error
    } finally {
        try { if (rs != null) rs.close(); } catch (Exception e) {};
        try { if (stmt != null) stmt.close(); } catch (Exception e) {};
    }

    ... but I didn't get it to work. I couldn't find out how to call the Oracle package with "begin ? := cmt.db_info; end;" that way. I got an error like 'missing parameter' or similar, so I ditched that for the time beeing.

    The connection is established at startup (simple login to the db) and then I pass the connection between the classes and methods. It feels a bit awkward to do it that way, but it was the only way I could think of. I refresh the window every 5 sec so I believe I can't close the connection after every refresh.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Oracle cursor will not close

    Well I'm only asking for debugging reasons, not as a suggestion for changing your design. For example, if the cursor goes away when the connection is closed, that would be good news. If the cursor stays, that could indicate that removing the cursors might be out of our control (at least on the face of it, there might be a way, but it would probably be complex).

    Another thing you could try: if you perform a garbage collection (System.gc()) after closing the ResultSet and waiting a few seconds before checking the cursor, does the cursor go away? Perhaps closing the result set does not explicitly remove the cursor, and it is held on to until java says otherwise (which would most likely occur during garbage collection).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Oracle cursor will not close

    How do you know it's an issue with the database cursor? Have you printed out the row number for every time you call next()?

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Oracle cursor will not close

    Quote Originally Posted by aussiemcgr View Post
    Well I'm only asking for debugging reasons, not as a suggestion for changing your design. For example, if the cursor goes away when the connection is closed, that would be good news. If the cursor stays, that could indicate that removing the cursors might be out of our control (at least on the face of it, there might be a way, but it would probably be complex).

    Another thing you could try: if you perform a garbage collection (System.gc()) after closing the ResultSet and waiting a few seconds before checking the cursor, does the cursor go away? Perhaps closing the result set does not explicitly remove the cursor, and it is held on to until java says otherwise (which would most likely occur during garbage collection).
    The cursors do close when I close the connection.

    I tried to perform an explicit garbage collection but with no result.

    --- Update ---

    Quote Originally Posted by copeg View Post
    How do you know it's an issue with the database cursor? Have you printed out the row number for every time you call next()?
    I monitor the open cursors in the database and they increase every time I do a 'prepSql(conn, query)' which is the expected behaviour. It all ends with a sql exception (ORA-01000 maximum open cursors exceeded). It's quite obvious where the problem lies since I fetch 8 resultsets for every refresh whitch adds 8 cursors in the database, but they will not release.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Oracle cursor will not close

    Ok. Maybe we can be creative. You might be able to do something like setting up a cache or something and adding the ResultSets to the cache each time you create them. Then, at the end of your program, go through the cache and check their statuses (if they are still there) to see if they were closed correctly.
    Just to clarify, are you printing the stack trace in EVERY catch block for exceptions? If not, do that first so we are sure we are not ignoring any other exceptions.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Oracle cursor will not close

    Quote Originally Posted by magher View Post
    I monitor the open cursors in the database and they increase every time I do a 'prepSql(conn, query)' which is the expected behaviour. It all ends with a sql exception (ORA-01000 maximum open cursors exceeded). It's quite obvious where the problem lies since I fetch 8 resultsets for every refresh whitch adds 8 cursors in the database, but they will not release.
    That explains it better, at least for me (I was misinterpreting how you were defining Cursor). If you close the ResultSet and Statement, then the resources should be released, but it seems closing the Connection releases them, so my recommendation would be to a) Close and reopen the Connection every iteration b) print or log all stack traces - even the ones caught when closing objects in the finally clause c) if you don't wish to close/reopen the connection each time, then post an SSCCE: the above code implies you are closing the ResultSet twice, making it hard (for me at least) to follow the logic - if the logic is complex there may be a leak from a ResultSet/Statement not getting closed somewhere along the line. An SSCCE would address this.

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Oracle cursor will not close

    And while you are at it, can you post your prepSql(conn, query); method?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Oracle cursor will not close

    Hi,

    Thank you all. The problem was mainly some seriously damaged braincells on my side, and and a few bugs in the code. It works now, case is solved (the code anyways) and I will be back with an explanation later.

    //Cheers

Similar Threads

  1. Virtual Cursor?
    By cgskook in forum Java Theory & Questions
    Replies: 11
    Last Post: May 25th, 2013, 02:39 AM
  2. Changing Cursor Properly
    By tukhes in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 4th, 2011, 01:03 PM
  3. Replies: 7
    Last Post: June 26th, 2011, 11:16 AM
  4. changing the splitpane's cursor
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 8th, 2010, 11:11 AM
  5. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 09:02 AM