所以基本上我有一个 Soap Web 服务,它可以从 SqlServer 数据库中插入和检索一些数据。
该网络服务使用一个负责数据库内容的单例。
public class Service : System.Web.Services.WebService
{
private DBAccess dbaccess;
public Service()
{
dbaccess = DBAccessLocalhost.GetInstance();
}
[WebMethod]
public List<Profile> XXX(Guid a, uint b, DateTime c)
{
return dbaccess.XXX(a, b, c);
}
...
}
访问数据库的单例。它有很多方法基本上可以做到这一点。
public class DBAccessLocalhost : DBAccess
{
private static DBAccess instance = null;
private string connectionString;
public static DBAccess GetInstance()
{
if (instance == null)
instance = new DBAccessLocalhost();
return instance;
}
private DBAccessLocalhost()
{
connectionString = "Data Source=localhost;Initial Catalog=DBName;Integrated Security=True;Max Pool Size=2000;Pooling=false";
}
public override void XXX(Guid a, uint b, DateTime c)
{
SqlCommand cmd;
SqlDataReader dr;
string strSql = "SP_Name";
SqlConnection conn;
conn = new SqlConnection(connectionString);
try
{
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@a", a.ToString());
cmd.Parameters.AddWithValue("@b", (int)b);
cmd.Parameters.AddWithValue("@c", c);
dr = cmd.ExecuteReader();
while (dr.Read() && dr.HasRows)
{
//Do stuff...
}
dr.Close();
}catch (Exception ex)
{
throw new DBError("Errors: " + ex.Message);
}
finally
{
conn.Close();
}
}
第二个版本:
public class DBAccessLocalhost : DBAccess
{
private static DBAccess instance = null;
private string connectionString;
public static DBAccess GetInstance()
{
if (instance == null)
instance = new DBAccessLocalhost();
return instance;
}
private DBAccessLocalhost()
{
connectionString = "Data Source=localhost;Initial Catalog=DBName;Integrated Security=True;Max Pool Size=2000;Pooling=true";
}
public override void XXX(Guid a, uint b, DateTime c)
{
string strSql = "SP_Name";
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(strSql, conn))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@a", a.ToString());
cmd.Parameters.AddWithValue("@b", (int)b);
cmd.Parameters.AddWithValue("@c", c);
try
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
//....
}
}
}
catch (Exception ex)
{
throw new DBError("Error: " + ex.Message);
}
finally
{
conn.Close();
}
}
}
问题是有时我会遇到这个异常:
DBAccessWebSerice,System.ServiceModel.FaultException: Server was unable to process
request. ---> Errors: ExecuteNonQuery requires an open and available
Connection. The connection's current state is closed (Sometimes says connecting).
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime
operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway,
ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
问题可能是因为同时有很多连接到数据库?
可能这不是最好的方法。但是如果你有更好的方法或者有解决这个问题的方法,请说。