Solved

Oracle ORA-01000: maximum open cursors exceeded

Posted on 2009-07-09
7
896 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

15 Experts available now in Live!

Get 1:1 Help Now