java - 独立应用程序的数据源 - 没有应用程序服务器 [英] java - DataSource for standalone application - no application server

查看:32
本文介绍了java - 独立应用程序的数据源 - 没有应用程序服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从独立应用程序连接到 MySQL 数据库.没有应用程序/网络服务器.

I want to connect to a MySQL database from a standalone application. There is no application/web server.

这个页面建议我应该使用 DriverManager 来创建连接.但是 this 页面表明已创建连接来自 DriverManager 的无法合并.

This page suggests that I should use DriverManager to create the connection. However this page suggests that connections created from DriverManager cannot be pooled.

但是连接池对我来说是必须的,因为应用程序使用多个线程并发写入数据库,因此关闭/重新创建连接不是最佳的.

However connection pooling is a must for me because the application uses various threads to write to the database concurrently, so closing/recreating connections is not optimal.

  1. 为此我应该使用哪个 DataSource 实现?我查看了 MysqlConnectionPoolDataSource 但我是不确定它是否真的实现了连接池.我还查看了 Tomcat DataSource 我确信它实现了一个连接池,但我不确定它是否可以在 Tomcat 之外使用.

  1. Which DataSource implementation should I use for this purpose? I looked at MysqlConnectionPoolDataSource but I am not sure if it actually implements a connection pool. I also looked at the Tomcat DataSource which I am sure implements a connection pool but I am not sure if it can be used outside Tomcat.

当我关闭独立应用程序时,我应该怎么做才能正确释放连接/池?

When I close the standalone application what should I do to release the connections/pool properly?

看起来 Tomcat 数据源 可用 来自独立应用程序,所以我将使用它.它还有一个关闭方法,我认为它会释放连接.

It looks like the Tomcat DataSource is usable from standalone application, so I am going to use it. It also has a close method which I assume will release the connections.

推荐答案

看看 Apache DBCP 和 DBUtils

Take a look at Apache DBCP and DBUtils

DBCP 将为您提供连接池功能等

DBCP will give you features for connection pool etc.

DBUtils 将使 jdbc 对您更容易.

DBUtils will make jdbc easier for you.

如前所述 DBPool 也是一个,还有 BoneCP 和大量池化工具.

As mentioned DBPool is also one, there is BoneCP and plenty pooling tools out there.

看一些示例:

这篇关于java - 独立应用程序的数据源 - 没有应用程序服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆