我正在使用Db
ServiceStack 服务中的属性来访问我的数据库,但我时不时地从 IIS 收到以下错误:
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
堆栈跟踪:
[InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.]
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +6371713
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6372046
System.Data.SqlClient.SqlConnection.Open() +300
ServiceStack.OrmLite.OrmLiteConnection.Open() +44
ServiceStack.OrmLite.OrmLiteConnectionFactory.OpenDbConnection() +132
ServiceStack.ServiceInterface.Service.get_Db() +68
我已经设置了ReuseScope
in the Configure
方法ReuseScope.None
我相信哪个应该根据每个请求关闭连接?我在这里做错了什么?
public override void Configure(Container container)
{
JsConfig.EmitCamelCaseNames = true;
//Register all your dependencies
ConfigureDb(container);
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
}
配置数据库:
private static void ConfigureDb(Container container)
{
var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider))
.ReusedWithin(ReuseScope.None);
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
// Do database seed/initialisation
}
}
Edit
经过进一步诊断,当我调用该服务方法时,多次刷新页面时,似乎会发生这种情况:
public Warranty Get(Warranty request)
{
var warranty = new Warranty();
if (request.Id != default(int))
{
warranty = Db.Id<Warranty>(request.Id);
warranty.WarrantyOrder = ResolveService<WarrantyOrderService>().Get(new WarrantyOrder { WarrantyId = warranty.Id });
warranty.WarrantyStatus = ResolveService<WarrantyStatusService>().Get(new WarrantyStatus { Id = warranty.StatusId });
warranty.WarrantyNotes = ResolveService<WarrantyNoteService>().Get(new WarrantyNotes { WarrantyId = warranty.Id });
warranty.WarrantyDialogues = ResolveService<WarrantyDialogueService>().Get(new WarrantyDialogues { WarrantyId = warranty.Id });
warranty.WarrantyCredit = ResolveService<WarrantyCreditService>().Get(new WarrantyCredit { WarrantyId = warranty.Id });
warranty.WarrantyPhotos = ResolveService<WarrantyPhotoService>().Get(new WarrantyPhotos { WarrantyReference = warranty.WarrantyReference });
warranty.WarrantyReport = ResolveService<WarrantyReportService>().Get(new WarrantyReport { WarrantyId = warranty.Id });
}
return warranty;
}
我变了ConfigureDb
根据@mythz 的回答如下:
private static void ConfigureDb(Container container)
{
var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
}
该服务需要调用其他服务来填充我的其他对象Warranty
对象,我不确定如何改进这个?