我需要在我的 (grails) Web 应用程序中使用 C3P0 池库吗?
我对连接池库一点也不熟悉。我刚刚通过这篇博客文章发现了它,我不确定我应该在我的基于 grails/hibernate/mysql 的 Web 应用程序中使用一个。
所以我的问题很简单:在什么情况下您会建议将连接池库集成到 grails 应用程序中?始终、从不或仅超过某些连接阈值?
PS:如果您曾经在 Web 应用程序中成功使用过 C3P0,我将非常感谢听到您的反馈(就可见的积极效果而言)。
I am not familiar at all with connection pooling library. I've just discovered it through this blog article) and I am not sure that I should use one in my web application based on grails/hibernate/mysql.
So my question is simple : in which situations would you suggest to integrate a connection pooling library into a grails application? Always, Never or only over some connections threshold?
P.S. : If you have ever used successfully C3P0 in your web application, I will greatly appreciate to hear your feedback (in terms of visible positive effects).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论哪种池实现,您都应该在 Web 应用程序中始终使用连接池。打开与数据库的连接是一项非常昂贵的任务,并且能够重用现有的空闲连接可以极大地提高站点性能。
连接可以由应用程序服务器(Tomcat、JBoss、Glassfish...)或您的应用程序管理。后者更容易设置,但很难根据部署进行自定义。在应用程序上配置连接池并将站点设置为使用它可以轻松微调连接池参数,例如:保持打开的最小连接数、最大空闲时间等。
Regardless of which pooling implementation, you should use a connection pool always in your web application. Open a connection with the database is a very expensive task and being able to reuse a already existing and idle connection greatly improves your site performance.
A connection can be managed by the application server (Tomcat, JBoss, Glassfish...) or by your application. The latter is easier to setup but it's hard to customize per deployment. Configuring a connection pool on the application and setting your site to consume it makes easy to the fine tune the connection pool parameters, like: minimum connections to keep open, max idle time and so on.
我对此的经验非常有限,但我最终使用了 C3P0,原因很简单,Hibernate 似乎无法处理 MySQL 重新启动。我每天早上都会遇到“管道损坏”的情况,因为我们的托管服务每天晚上都会重新启动 MySQL。
我用谷歌搜索了它,我能找到的唯一建议是使用...应用程序服务器或 C3P0 的连接池。对我来说,后者效果很好。
My experience with this is pretty limited, but I ended up using C3P0 for the simple reason that Hibernate does not seem to handle MySQL restarts. I got a "Broken pipe" every morning because our hosting service restarted MySQL every night.
I googled it and the only advice I could find was to use... the connection pool of the app server or C3P0. For me, the latter works just fine.
我总是使用连接池有两个原因:
如果您已经在使用 hibernate,只需修改 hibernate.cfg.xml 的 connection.provider_class 以使用 org.hibernate.connection.C3P0ConnectionProvider 并将 c3p0 jar 文件放入您的servlet 的 WEB-INF/lib 文件夹。完毕。
如果您使用 JNDI 和 GlobalNamingResources 声明,请将 type 属性修改为指向 com.mchange.v2.c3p0.ComboPooledDataSource 并将 c3p0 jar 放入 Tomcat 的 /lib文件夹。完毕。
I always use a connection pool for two reasons:
If you're already using hibernate, just modify your hibernate.cfg.xml's connection.provider_class to use org.hibernate.connection.C3P0ConnectionProvider and throw the c3p0 jar file into your servlet's WEB-INF/lib folder. Done.
If you're using JNDI and a GlobalNamingResources declaration, modify the type property to point to com.mchange.v2.c3p0.ComboPooledDataSource and throw the c3p0 jar into Tomcat's /lib folder. Done.
C3P0 是一个非常不错的池,但我仍然建议使用应用程序服务器或 servlet 引擎的连接池,并将 Grails 配置为通过常规数据源使用它。当您无法做到这一点时,请使用独立的连接池(在这种情况下,C3P0 是一个不错的选择)。
C3P0 is a very decent pool but I would still recommend to use the connection pool of your app server or servlet engine and to configure Grails to use it via a regular DataSource. Use a stand-alone connection pool when you can't do that (in which case C3P0 is a good choice).