Link to home
Start Free TrialLog in
Avatar of pzaprianov
pzaprianovFlag for United States of America

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That all looks a bit strange. Could you post your actual sql?
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pzaprianov

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I put everything in finally block and it worked, and now I will implement merge to make my code cleaner, thank you guys.