Connection.State Issue
I've published my new website , In my computer it works fine and no problem , But in the Server I when some user connect at the same time it crash .
I found out There is a error at this Method :
public static DbDataReader ExecuteReader(DbCommand dbCommand, CommandBehavior commandBehavior)
{
if (dbConnection.State != ConnectionState.Open)
dbConnection.Open();
return dbCommand.ExecuteReader(commandBehavior);
}
When i trace it , It says ConnectionState is not open , and it's doing opening . Here are my questions :
1- Is it a problem if while ConnectionState is doing opening , we Open the connection again ? 2- What I've missed , that i receive this error ?Edit
For more information I past some part of my code here :
public class DbProviderHelper
{
private static DbProviderFactory dbProviderFactory;
private static DbConnection dbConnection;
#region dbConnexion
public static DbConnection GetConnection()
{
if (dbConnection == null)
{
ConnectionStringsSection connectionStringsSection = GetConnectionStringsSection();
dbProviderFactory = DbProviderFactories.GetFactory(connectionStringsSection.ConnectionStrings[1].ProviderName);
dbConnection = dbProviderFactory.CreateConnection();
dbConnection.ConnectionString = connectionStringsSection.ConnectionStrings[1].ConnectionString;
}
return dbConnection;
}
public static ConnectionStringsSection GetConnectionStringsSection()
{
return ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;
}
#endregion dbConnexion
#region dbCommand
public static DbCommand CreateCommand(String commandText, CommandType commandType)
{
DbCommand dbCommand = dbProviderFactory.CreateCommand();
dbCommand.Connection = dbConnection;
dbCommand.CommandType = commandType;
dbCommand.CommandText = commandText;
return dbCommand;
}
#endregion dbCommand
#region dbParameter
public static DbParameter CreateParameter(string parameterName, DbType dbType, object value)
{
DbParameter oDbParameter = dbPr开发者_如何学编程oviderFactory.CreateParameter();
oDbParameter.ParameterName = parameterName;
oDbParameter.DbType = dbType;
oDbParameter.Value = value;
return oDbParameter;
}
#endregion dbParameter
#region Operations
public static DbDataReader ExecuteReader(DbCommand dbCommand)
{
if (dbConnection.State != ConnectionState.Open)
dbConnection.Open();
return dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
public static int ExecuteNonQuery(DbCommand dbCommand)
{
try
{
if (dbConnection.State != ConnectionState.Open)
dbConnection.Open();
return dbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
dbConnection.Close();
}
}
#endregion Operations
}
And i invote that like :
public class Configuration
{
public Configuration()
{
DbProviderHelper.GetConnection();
}
public DbDataReader GetTabsParent(int tabId)
{
DbCommand oDbCommand = DbProviderHelper.CreateCommand("Portal_TabsGetParent", CommandType.StoredProcedure);
oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TabID", DbType.Int32, tabId));
DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
return oDbDataReader;
}
}
Is it a problem if while ConnectionState is doing opening , we Open the connection again?
Yes, it can throw an exception.
Straight from MSDN: SqlConnection.Open method
InvalidOperationException
:
Cannot open a connection without specifying a data source or server.
or
The connection is already open
(are you executing the method on different threads with a single shared connection?)
精彩评论