简体   繁体   English

J2SE上的数据源和DriverManager

[英]DataSource and DriverManager on J2SE

I'm interested in developing a desktop application that connects to a MySQL DB. 我对开发连接到MySQL数据库的桌面应用程序感兴趣。 After reading this java tutorial on DB connection (and several others) I have a question. 看完对数据库连接(和其他几个人)Java教程我有一个问题。 Using DataSource seems popular on J2EE while DriverManager is a common choise for desktop applications. 在J2EE上,使用DataSource似乎很流行,而DriverManager是桌面应用程序的常见选择。 Is it still possible to use DataSource on a desktop application? 仍然可以在桌面应用程序上使用DataSource吗? If yes, is it a better choice over DriverManager? 如果是,那是不是优于DriverManager的更好选择?

It's perfectly possible to use a DataSource on a desktop application. 完全有可能在桌面应用程序上使用数据源。

Server apps are usually multi-thread, multi-user applications, where several connections to the database are open in parallel. 服务器应用程序通常是多线程,多用户应用程序,其中与数据库的多个连接并行打开。 A pooled DataSource is critical here. 池化数据源在这里至关重要。

Desktop apps are usually single-user applications, where you just need one connection to the database. 桌面应用程序通常是单用户应用程序,您只需要一个数据库连接即可。 So a pooled datasource isn't necessary in this case. 因此,在这种情况下,不需要合并数据源。

I'll quote the javadoc: 我将引用javadoc:

An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection. 作为DriverManager工具的替代方法,DataSource对象是获得连接的首选方法。

Normally you use DriverManager when you just want a connection for one time while with DataSource you get other features such as connection pooling and distributed transactions. 通常,当您只想连接一次时使用DriverManager ,而使用DataSource可以获得其他功能,例如连接池和分布式事务。

Hope this helps. 希望这可以帮助。

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

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