如何在 Java 的 DataSource 中实现 getConnection()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4075778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I implement getConnection() in DataSource in Java?
提问by Danny
I'm reading up on DataSource, here, and trying to implement it in my own little project by using a simple file as my "data source". I've created a class that is pretty simple at the moment...
我正在阅读 DataSource,here,并尝试通过使用一个简单的文件作为我的“数据源”在我自己的小项目中实现它。我创建了一个目前非常简单的类......
public class QueueData implements DataSource { ... }
though the reason it is simple is because I haven't been able to find a resource that explains how the implemented methods should work. Everyone seems to just list the initialization of a context and a magical getConnection() call, like so.
虽然原因很简单是因为我一直无法找到解释所实现的方法应该如何工作的资源。每个人似乎都只是列出了上下文的初始化和神奇的 getConnection() 调用,就像这样。
Context ctx = new InitialContext(env1);
DataSource ds = (DataSource)ctx.lookup("jdbc/mydatasource");
Connection conn = ds.getConnection(); // Magical method!
But can one of you actually give me an example of what the code inside getConnection() should look like?
但是你们中的一个人真的可以给我一个例子来说明 getConnection() 中的代码应该是什么样子的吗?
采纳答案by Thilo
The reason no one shows samples how to implement a DataSource and "just" uses them instead is because only JDBC driver vendors (usually database makers) need to write them.
没有人展示如何实现数据源的示例并“只是”使用它们的原因是因为只有 JDBC 驱动程序供应商(通常是数据库制造商)需要编写它们。
What it should do is of course return a Connection object that will also need to be an instance of of a driver-specific class. In your case, something that can accept SQL statements to read from a file.
它应该做的当然是返回一个 Connection 对象,该对象也需要是特定于驱动程序的类的实例。在您的情况下,可以接受 SQL 语句从文件中读取的东西。
Your code could look something like this:
您的代码可能如下所示:
public Connection getConnection(){
final String fileName = getFileNameFromMyDatabaseUrl();
return new MyFileConnection(new File(fileName));
}
Which of course is not very interesting code, either.
这当然也不是很有趣的代码。
You can look at some open-source DataSource implementations to see what they do:
您可以查看一些开源 DataSource 实现以了解它们的作用:
- Apache Commons DBCP PoolingDataSource(a connection pool)
- Apache Derby's EmbeddedDataSource(an embedded database written in Java)
- Postgresql's BaseDataSource(abstract base class for the Postgresql JDBC driver)
- Apache Commons DBCP PoolingDataSource(一个连接池)
- Apache Derby 的EmbeddedDataSource(用 Java 编写的嵌入式数据库)
- Postgresql 的BaseDataSource(Postgresql JDBC 驱动程序的抽象基类)
回答by chiccodoro
Connection
is pretty sure an interface itself, which you will need to implement as well. Then, your getConnection()
will return an instance of your own class. However, be prepared for a great deal of work... (e.g., if you want to be able to get the datasource over the context, you'll need to register your implementation first.)
Connection
非常确定一个接口本身,您也需要实现它。然后,您getConnection()
将返回您自己的类的一个实例。但是,要为大量工作做好准备……(例如,如果您希望能够通过上下文获取数据源,则需要先注册您的实现。)
Why do you want to develop your own DataSource? There are very lightweight file-based and in-process database libraries available on the web (although I have to admit I don't know them too well), as for example HSQLDB(Wikipedia article), H2(Wiki)...
为什么要开发自己的DataSource?网络上有非常轻量级的基于文件和进程内的数据库库(尽管我不得不承认我不太了解它们),例如HSQLDB(维基百科文章)、H2(维基)......
If you were developing in C#, I'd also recommend to study Linq To XML, but I don't know whether the Java world has already come up with a counterpart for that...
如果您使用 C# 进行开发,我还建议您学习 Linq To XML,但我不知道 Java 世界是否已经为此提出了对应的...
回答by sasuke
It's not magical. You retrieve a DataSource
object which was bound to the JNDI server, typically when you setup your connection pool in your application server. This setup requires you to provide all the necessary database details like connection url, authentication credentials and other options like the DataSource
class for that particular database (which is present in the jdbc driver which comes with that database). This setup information is utilized to create a DataSource
which knows how to hand out a connection for that given database.
这并不神奇。您检索DataSource
绑定到 JNDI 服务器的对象,通常是在您在应用程序服务器中设置连接池时。此设置要求您提供所有必要的数据库详细信息,例如连接 url、身份验证凭据和其他选项,例如该DataSource
特定数据库的类(存在于该数据库附带的 jdbc 驱动程序中)。此设置信息用于创建一个DataSource
知道如何为给定数据库分发连接的信息。
Let's say that you want to write your own implementation which gives the client a NOOP connection (connection object on which all operations yield nothing). All you have to do is "register" a custom DataSource
implementation with the app servers JNDI server. That implementation's getConnection()
method would just return a class which implements the Connection interface and methods which do nothing.
假设您想编写自己的实现,它为客户端提供一个 NOOP 连接(所有操作都不产生任何结果的连接对象)。您所要做的就是DataSource
向应用服务器 JNDI 服务器“注册”一个自定义实现。该实现的getConnection()
方法只会返回一个实现 Connection 接口的类和什么都不做的方法。