问题
I'm facing
ORA-01000: maximum open cursors exceeded
After hosting an ASP web page on IIS. When I test the web page with visual studio. The maximum open cursors exceeded problem does not happen.
- What the cause of the problem happen & how can I solve it
- When I close and displose the Oracle connection then will the opened cursors be automatically closed? If not, how can I close them?
Code I use to close & displose the connection
rdr.Close()
rdr.Dispose()
cmd.Connection.Close()
cmd.Connection.Dispose()
For Each para As OracleParameter In cmd.Parameters
para.Dispose()
Next
cmd.Dispose()
con.Close()
con.Dispose()
Is my usage correct?
回答1:
Based upon you responses to the other answers, your problem is most likely the IIS web server is using a connection pool for your database connection. A connection pool will create a number of connections and, from the database perspective, keep them open for a long time. It does this because the assumption being made is opening a database connection is time consuming in relation to the duration of displaying a page.
You need to review the configuration of the IIS and either disable the connection pool (not recommended), or make the pool size smaller.
回答2:
It's hard to say what's going here for sure without more code, but I can make a good guess: you're not closing your connections properly!
Anywhere you see a manual call to .Close() or .Dispose() in regards to database connections, these calls MUST be located in a finally block (for preference, the implicit finally block created for a using statement). Otherwise, if any exceptions are thrown by your database code you might skip past the .Close() calls, thus leaving the connection hanging open and therefore vulnerable to things like your max open cursor problem.
It should look more like this:
using (var con = new OracleConnection(...))
using (var cmd = new OracleCommand(" sql here ", con))
{
cmd.Parameters.Add( ... ).Value = ...
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
//...
}
}
}
I know I skipped past the some things in there (...), but that's the gist. I did not skip past any calls to .Close() or .Dispose(). The using blocks take care of that for us automatically.
回答3:
I can't telly you if you use asp correctly or not as the code you posted is not complete. However the open_cursors init.ora parameter determines how many cursors can be opened by a database session at once. To determine if you are leaking cursors or if the parameter is set too low the first step is to determine the current value of the init.ora parameter open_cursors.
To determine which cursors are open (potentially forgotten to be closed). you can use the view v$open_cursor.
回答4:
I have a batch that do many request to bd with the same oracle connection. To resolve the problem, i close my ref_cursor after the rdr.read(). It's work for me. Just rdr.close()
回答5:
Dispose the OracleCommand:
OracleCommand cmd = new OracleCommand();
cmd.Dispose();
However you also need to dispose and close the OracleDataReader:
OracleDataReader DataReader = Util.ExecuteDataForQuery(); // this returns the OracleDataReader object
// use it for your purpose
DataReader.Close();
DataReader.Dispose();
来源:https://stackoverflow.com/questions/14150311/ora-01000-maximum-open-cursors-exceeded-in-asp-net