如何在 Java 中的 DataSource 中实现 getConnection() ?

发布于 2024-09-30 02:10:49 字数 624 浏览 0 评论 0原文

我正在阅读有关 DataSource 的内容,此处,并尝试使用一个简单的文件作为我的“数据源”在我自己的小项目中实现它。我目前创建了一个非常简单的类...

public class QueueData implements DataSource { ... }

尽管它之所以简单是因为我无法找到解释所实现的方法应该如何工作的资源。每个人似乎都只是列出了上下文的初始化和神奇的 getConnection() 调用,就像这样。

    Context ctx = new InitialContext(env1);
    DataSource ds = (DataSource)ctx.lookup("jdbc/mydatasource");
    Connection conn = ds.getConnection(); // Magical method!

但是你们中的一个人能给我一个 getConnection() 内部代码应该是什么样子的例子吗?

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...

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.

    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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

々眼睛长脚气 2024-10-07 02:10:49

没有人展示如何实现 DataSource 并“只是”使用它们的示例的原因是因为只有 JDBC 驱动程序供应商(通常是数据库制造商)需要编写它们。

当然,它应该做的是返回一个 Connection 对象,该对象也需要是特定于驱动程序的类的实例。在你的例子中,可以接受 SQL 语句从文件中读取的东西。

您的代码可能如下所示:

 public Connection getConnection(){
      final String fileName = getFileNameFromMyDatabaseUrl();
      return new MyFileConnection(new File(fileName));
 }

这当然也不是非常有趣的代码。

您可以查看一些开源 DataSource 实现来了解它们的作用:

  • Apache Commons DBCP PoolingDataSource (连接池)
  • Apache Derby 的 EmbeddedDataSource (用 Java 编写的嵌入式数据库)
  • Postgresql 的 < a href="http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/postgresql/ds/common/BaseDataSource.java?rev=1.21&content-type=text/x -cvsweb-markup" rel="nofollow">BaseDataSource (Postgresql JDBC 驱动程序的抽象基类)

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.

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.

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:

波浪屿的海角声 2024-10-07 02:10:49

这并不神奇。您检索绑定到 JNDI 服务器的 DataSource 对象,通常是在应用程序服务器中设置连接池时。此设置要求您提供所有必要的数据库详细信息,例如连接 url、身份验证凭据和其他选项,例如该特定数据库的 DataSource 类(存在于该数据库附带的 jdbc 驱动程序中)。此设置信息用于创建知道如何为给定数据库分配连接的DataSource

假设您想要编写自己的实现,为客户端提供 NOOP 连接(所有操作都不会产生任何结果的连接对象)。您所要做的就是向应用服务器 JNDI 服务器“注册”自定义 DataSource 实现。该实现的 getConnection() 方法只会返回一个实现 Connection 接口的类和不执行任何操作的方法。

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.

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.

赤濁 2024-10-07 02:10:49

Connection 本身就是一个接口,您也需要实现它。然后,您的 getConnection() 将返回您自己的类的实例。但是,要做好大量工作的准备...(例如,如果您希望能够通过上下文获取数据源,则需要首先注册您的实现。)

为什么要开发自己的数据源?网络上有非常轻量级的基于文件和进程内数据库(尽管我不得不承认我不太了解它们),例如 HSQLDB维基百科文章),H2 (Wiki)...

如果你是用C#开发的话,我也推荐学习Linq To XML,但是我不知道Java世界是否已经拿出了一个对应的...

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.)

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)...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文