首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >超时过期了。-在ServiceStack服务中使用Db

超时过期了。-在ServiceStack服务中使用Db
EN

Stack Overflow用户
提问于 2013-11-12 11:20:46
回答 1查看 1.6K关注 0票数 6

我正在使用Db服务中的ServiceStack属性来访问我的数据库,但是我不时地从ServiceStack中得到以下错误:

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.

堆栈跟踪:

代码语言:javascript
复制
[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

我已经将Configure方法中的Configure设置为ReuseScope.None,它应该根据每个请求关闭连接吗?我在这里做错什么了?

代码语言:javascript
复制
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));
}

ConfigureDb:

代码语言:javascript
复制
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
    }
}

编辑

在进行了更多的诊断之后,当我多次调用此服务方法时,似乎会多次刷新页面:

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
    private static void ConfigureDb(Container container)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
        container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
    }

该服务需要调用其他服务来填充我的Warranty对象上的其他对象,我不知道如何改进它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-12 11:26:10

与所有连接管理器一样,IDbConnectionFactory是一个线程安全的工厂,用于创建DB连接,也就是说,它本身不是DB连接。它应该登记为单身人士。

默认情况下,ServiceStack的IOC (Funq)默认注册为Singleton,因此正确的注册只是:

代码语言:javascript
复制
container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));

解析服务

由于ServiceStack服务具有利用托管资源的潜力,因此无论何时从另一个服务解析它们,都应该在using语句中使用它们,以便适当地处理它们,例如:

代码语言:javascript
复制
using (var orders = ResolveService<WarrantyOrderService>())
using (var status = ResolveService<WarrantyStatusService>())
{
    var warranty = new Warranty { 
        WarrantyOrder = orders.Get(new WarrantyOrder { WarrantyId = warranty.Id }),
        WarrantyStatus = status.Get(new WarrantyStatus { 
            WarrantyId = warranty.StatusId }),
       //etc
    }       

    return warranty; 
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19927765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档