Solved

Oracle ORA-01000: maximum open cursors exceeded

Posted on 2009-07-09
7
918 Views
Last Modified: 2012-06-27
Hello all,

My program is searching in the DB and if it finds a data will update otherwise will insert.

I am sure that I am closing every single ResultSet and PrepareStatement but I still get the ORA-01000: maximum open cursors exceeded, If I do it with less iterations it works.

Any suggestions?

Thank you guys
for (int i = 1; i < 100; i++) {
 
				for (int z = 1; z < 10; z++) {
					PreparedStatement pstmSelect = conOracle
							.prepareStatement(select);
					pstmSelect.setObject(1, obj);
					ResultSet rs = null;
					try {
						rs = pstmSelect.executeQuery();
					} catch (SQLException e) {
                                                rs.close();
                                                pstmSelect.close();
						logger.error();
						continue;
					}
 
					if (rs.next()) {
 
						// Update statement
						PreparedStatement pstmUpdate = conOracle
								.prepareStatement(update);
						pstmUpdate.setObject(1, obj);
						try {
							pstmUpdate.executeUpdate();
							pstmUpdate.close();
							pstmUpdate = null;
						} catch (SQLException e) {
							pstmUpdate.close(); 
							logger.error(); 
						}
					} else {
						PreparedStatement pstmInsert = conOracle
								.prepareStatement(insert);
						pstmInsert.setObject(1, obj);
						}
 
						try {
							pstmInsert.executeUpdate();
							pstmInsert.close();
							pstmInsert = null;
						} catch (SQLException e) {
							pstmInsert.close(); 
							logger.error(); 
						}
					}
					rs.close();
					rs = null;
					pstmSelect.close();
					pstmSelect = null;
                              }
                       }

Open in new window

0
Comment
Question by:pzaprianov
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24816849
That all looks a bit strange. Could you post your actual sql?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24817075
I recommend using a JNDI data source and/or a connection pool. Regardless of how you do the connections, repeatedly opening and closing full connections is very inefficient, as I'm sure Charles will tell you. :)

Also, why are you using a prepared statement in a loop? You only need to prepare it once, that is the whole purpose of using PreparedStatement, then use execute many times on the same statement with new parameters.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 24817191
If you're using Oracle with update/insert, you might want to look at MERGE anyway:

http://www.oracle.com/technology/products/oracle9i/daily/Aug24.html
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:pzaprianov
ID: 24817889
I don't think my actual sql has something to do with the problem. Is the very simple select, insert and update.

I am using Prepared statement because I am leading with objects and I need to do commit with every single row because they may have dependency between them

I will try with merge it will make my code less hairy.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24817927
commit should not be a requirement. constraints / dependencies are in effect for the transaction based on the order of insert, not whether the row has been committed or not.

Frequent commits are bad for performance as well.

0
 
LVL 6

Assisted Solution

by:jwenting
jwenting earned 250 total points
ID: 24820467
Your code is flawed. You're closing a lot of your PreparedStatements only if an SQLException occurs.
If they're successfully executed you leave them open indefinitely, which will eventually lead to an ORA-01000.
Instead of closing the PreparedStatements in catch blocks, close them in finally blocks to ensure they're really gone.
0
 

Author Closing Comment

by:pzaprianov
ID: 31601772
I put everything in finally block and it worked, and now I will implement merge to make my code cleaner, thank you guys.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 60
difference of if loops 23 62
javap not working 8 59
Cisco ASA: Java web start no go, asdm launcher no go 3 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

827 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