Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle ORA-01000: maximum open cursors exceeded

Posted on 2009-07-09
7
Medium Priority
?
950 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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