简体   繁体   English

单例连接和数据库池

[英]Singleton connection and database pooling

I developed some web application. 我开发了一些Web应用程序。 In application I have now singleton pattern for recieve DB connection. 在应用程序中,我现在有单独的模式来接收数据库连接。 I realized that it is bad approach because after some hours I always get "MySQLNonTransientConnectionException". 我意识到这是不好的方法,因为几个小时后我总是得到“MySQLNonTransientConnectionException”。 So now I come with new solution of database pooling ( http://jolbox.com/ ). 所以现在我带来了新的数据库池解决方案( http://jolbox.com/ )。 But my problem is how to design it. 但我的问题是如何设计它。

Should I use singleton for connection pool and create one instance of pool in whole application? 我应该使用单例连接池并在整个应用程序中创建一个池实例吗?

Or I should always create new pool and then ask for connection from pool for every query? 或者我应该始终创建新池,然后从每个查询请求池连接?

The whole point of a connection pool is that you have one connection pool and then everything in your application requests connections from the pool when they need them. 连接池的重点是您有一个连接池,然后应用程序中的所有内容在需要时请求池中的连接。

So yes having one connection pool is the way forwards, whether you achieve that using a Singleton or the better option of dependency injection is up to you. 所以是的,有一个连接池是前进的方式,无论你是使用Singleton实现,还是更好的依赖注入选项取决于你。

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

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