简体   繁体   English

Web服务中Singleton类的影响

[英]Effect of Singleton classes in Webservices

I am implementing a webapplication and providing few rest APIs. 我正在实现一个Web应用程序,并提供了一些其他的API。

Following is the structure of my webapplication. 以下是我的Web应用程序的结构。

Service Layer 服务层
Service class contains different APIs. 服务类包含不同的API。

It invokes DAO which is a singleton class (I would rather say that it is an Enum and only instance has been defined for Enum ) and it does not have any state. 它调用DAO ,它是一个单例类 (我想说这是一个Enum,并且只为Enum定义了实例),并且它没有任何状态。 I mean there are no instance variable in DAO layer classes. 我的意思是DAO图层类中没有实例变量。

DAO Layer DAO层
DAO class fetches DB connection (DB connection is provided through Enum class, and I created the connection at the time of instantiating the Enum ), and it execute respective query on the DB. DAO类获取数据库连接 (通过Enum类提供数据库连接,而我在实例化Enum时创建了连接),并在数据库上执行相应的查询。

So Application architecture is like this. 所以应用程序架构就是这样。

ServiceClass {
    Dao.operation()
}

DaoEnum {

    instance;

    operation() {
        DBConnectionEnum.instance.connection.preparedStmt.respective operation
    }
}

DBConnectionEnum {
    DBConnectionEnum() {
        grabDBConnection()
    }
}

I understand I have to use connectionpool to grab db connection, but I am unable to understand the impacts caused by Dao layer, which is singleton. 我知道我必须使用connectionpool来获取数据库连接,但是我无法理解由Dao层(即单例)引起的影响。

Could you suggest me whether it is a correct design, or will it exhaust the application? 您能否建议我这是一个正确的设计,还是会耗尽应用程序? What improvements can be made to make application more robust, fast and performant? 可以进行哪些改进以使应用程序更健壮,快速且高性能?

the Singleton design pattern is a good pattern on its own, otherwise it would not have lasted this long. Singleton设计模式本身就是一个好的模式,否则它不会持续这么长时间。 There are some drawbacks , like introducing global state into your application. 有一些缺点 ,例如将全局状态引入您的应用程序。 But I don't think those downsides are enough to discount it. 但是我认为这些缺点不足以抵消它。 However there are better ways of doing what you're trying to do. 但是,有更好的方法可以做您想做的事情。 Dependency Injection is one way of abstracting invocation from implementation. 依赖注入是从实现中抽象调用的一种方法。 You can even use a Singleton in your DI Implementation, as long as it's transparent from the invocation. 您甚至可以在DI实现中使用Singleton,只要它对调用透明即可。 I would highly recommend that you do some research on DI. 我强烈建议您对DI进行一些研究。

I was discussing the issue with my colleague and as per the discussion I felt my singleton DAO layer must not cause any issues for me. 我正在与同事讨论此问题,根据讨论,我感到我的单例DAO层一定不会对我造成任何问题。

As DAO layer is working as a mediator between DB and Service class, and DAO layer do not maintain any state. 由于DAO层在DB和Service类之间充当中介者,因此DAO层不维护任何状态。 So when 2 similar requests are invoked simultaneously, then both requests will be served in 2 different threads and each thread will maintain its own Stack. 因此,当同时调用2个类似的请求时,两个请求将在2个不同的线程中得到服务,并且每个线程将维护自己的堆栈。 When both the requests call DAO layer, these requests will simply invoke DAO layer's method which will be associated with the respective thread's stack. 当两个请求都调用DAO层时,这些请求将简单地调用DAO层的方法,该方法将与相应线程的堆栈关联。

So If I create Singleton DAO layer in my application to serve multiple web requests, it will not exhaust throughput and clients must be served without any issue. 因此,如果我在应用程序中创建Singleton DAO层以服务多个Web请求,它将不会耗尽吞吐量,因此必须为客户端提供服务而不会出现任何问题。

Hope I am making little sense. 希望我没有任何意义。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM