Solved

Oracle ORA-01000: maximum open cursors exceeded

Posted on 2009-07-09
7
940 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 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
Industry Leaders: 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!

 

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

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.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

635 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