Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 958
  • Last Modified:

Oracle ORA-01000: maximum open cursors exceeded

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
pzaprianov
Asked:
pzaprianov
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
CEHJCommented:
That all looks a bit strange. Could you post your actual sql?
0
 
mrjoltcolaCommented:
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
 
CEHJCommented:
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.

 
pzaprianovAuthor Commented:
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
 
mrjoltcolaCommented:
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
 
jwentingCommented:
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
 
pzaprianovAuthor Commented:
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

Independent Software Vendors: 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!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now