System.Data.OracleClient random Invalid Operation the connection is closed
We are receiving an error from System.Data.OracleClient
: Invalid operation. The connection is closed.
The error appears randomly with an incidence about 1 in every 1000 operations.
What could be the problem?
Maybe i got the answer.
The connection object was used like this:
[Serializable] public abstract class BaseWizard : IBaseWizard {
[NonSerialized]
protected static ISession _session;
protected static ISession Session
{
get
{
if (_session == null)
_session = Session.Create(ConnectionType.DEFINED);
return _session;
}
}
}
(Session is a personalized dbconnection object )
and even though there was the check latter if (!Session.IsOpen) Session.Open there was a chance that after checking the state a before running the command some other operation might close the connection, and as you can see as the Session object is static the new operation will find the connection closed
The problem would be that if the connection would not be static that will mean a loot of o开发者_Python百科pened connection / session in the db
is there another way to fix this leaving the connection static?
Watch out for the transactions, they are the ones that were causing me this problem:
System.InvalidOperationException: Invalid operation. The connection is closed.
at System.Data.OracleClient.OracleConnection.GetOpenInternalConnection()
at System.Data.OracleClient.OracleConnection.get_ErrorHandle()
at System.Data.OracleClient.OracleDataReader.ReadInternal()
at System.Data.OracleClient.OracleDataReader.Read()
at Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDataReaderWrapper.Read()
The default TransactionScope timeout is 60 seconds, but my recording process was taking more than this sometimes (I should put only code to write to the DB on this process, but there is a lot of reading commands). So the exception was happening randomly... Changing the timeout value make this work. It took me so long to figure it out because the description of the error (Invalid operation. The connection is closed.) wasn't clear to link it with transaction issues.
I don't know if your problem is the same as mine, but that's how I handled that. Hope it helps you someway.
A number of conditions can cause a connection to close unexpectedly. Network issues, Garbage Collection, etc.
It is safer to wrap your data command in a conditional open to make sure that you have a good connection before using it.
if (this.mDBConnection.State != System.Data.ConnectionState.Open)
{
try
{
this.mDBConnection.ConnectionString = this.mDBConnectionString;
this.mDBConnection.Open();
}
catch (System.Exception ex)
{
ret = false;
throw ex;
}
}
While this doesn't answer the question "What is causing this?" it will hopefully give a solution to prevent it from being a further issue.
Cheers,
CEC
精彩评论