简体   繁体   English

Shell脚本中的Java DB连接池

[英]Java DB Connection Pooling in Shell Script

Does connection pooling db connections make sense when java code is called from a shell script, or is it better to use individual connections? 从Shell脚本调用Java代码时,连接池数据库连接是否有意义,还是使用单个连接更好? Doesn't the jvm exit after every call to the shell script, forcing the db pool/factory/etc to be re-created each time the script is called? 每次调用shell脚本后,jvm都不会退出,从而导致每次调用该脚本时都重新创建db pool / factory / etc吗?

For example, I have an external process that makes calls to a shell script, this shell script then calls a java class which executes 1 or more DB operations (query, insert, update, delete.), depending on the invoked operation. 例如,我有一个外部过程来调用Shell脚本,然后此Shell脚本调用一个Java类,该Java类根据所调用的操作执行1个或多个DB操作(查询,插入,更新,删除)。 This is a standalone batch process which does not use a servlet container like tomcat. 这是一个独立的批处理过程,不使用tomcat之类的servlet容器。 This shell script gets called by my external process over and over. 我的外部进程反复调用此shell脚本。

My environment is a bash shell script calling a java application (main() method) which utilizes spring jdbc and commons-dbcp 1.4 for it db processing and pooling. 我的环境是一个bash shell脚本,它调用Java应用程序(main()方法),该应用程序利用spring jdbc和commons-dbcp 1.4对其进行db处理和池化。

Thanks in advance! 提前致谢!

You should think about using a Connection Pool if you have so many calls to the database, that initializing Connections becomes costly. 如果您有太多的数据库调用,则应该考虑使用连接池,以至于初始化连接的成本很高。

Otherwise, for a batch process for which (or if) you can serialize the running tasks, one Connection would be sufficient. 否则,对于可以(或如果可以)序列化正在运行的任务的批处理过程,一个Connection就足够了。 Spring-JDBC also provides a single connection "pool" factory object for testing, which should work for you as well (so you don't have to deal with the boiler-plating). Spring-JDBC还提供了一个用于测试的连接“池”工厂对象,该对象也同样适用于您(因此您不必处理样板)。

In your case, a connection pool probably only has a limited benefit, since you rightly said that whenever the JVM is shut down and started again, you would have to create the connection pool again. 在您的情况下,连接池可能只能带来有限的好处,因为您正确地说过,每当JVM关闭并再次启动时,就必须再次创建连接池。 You could still use the pool if you want to benefit from using the DataSource interface instead of creating the connection directly. 如果您想从使用DataSource接口中受益而不是直接创建连接,则仍可以使用该池。 If your processing is done in a single thread, make sure that the size of the connection pool is only 1. 如果您的处理是在单个线程中完成的,请确保连接池的大小仅为1。

If you're using multiple threads in the same process, then the connection pool probably makes more sense and you could benefit from its features. 如果在同一进程中使用多个线程,则连接池可能更有意义,您可以从其功能中受益。

  1. Opening a database connection is very expensive affair both in terms of resources and performance (response time). 在资源和性能(响应时间)方面,打开数据库连接都是非常昂贵的事情。

  2. When you don't use connection pool , you obtain the connection, use it and then close it. 您不使用连接池时 ,可以获取连接,使用它然后关闭它。 This is repeated every time whenever a DB interaction is needed. 每当需要进行数据库交互时,都会重复此过程。

  3. A connection pool saves your from this by creating a pool of connection which can be reused, thus saving the time and resources of creating connection every time. 连接池通过创建可重复使用的连接池来节省您的时间,从而节省了每次创建连接的时间和资源。

  4. It does not matter whether you use servlet container or standalone application . 使用servlet容器还是独立应用程序都没有关系。 It depends on the way your application (web/standalone) is interacting the database. 这取决于您的应用程序(Web /独立)与数据库交互的方式。

  5. If your application has lot of parallel operation involving concurrent database access, using connection pool will be very useful. 如果您的应用程序有很多涉及并发数据库访问的并行操作,那么使用连接池将非常有用。

  6. So the choice and relevance of connection pool depend on the way application interacts with DB, irrespective of whether its web application or standalone. 因此,连接池选择和相关性取决于应用程序与DB交互的方式,而不管其Web应用程序还是独立的。

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

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