How do I implement getConnection() in DataSource in Java?

13,062

Solution 1

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:

Solution 2

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

Solution 3

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.

Share:
13,062
Danny
Author by

Danny

Software engineer that loves himself some music, art, video games, and stories.

Updated on June 04, 2022

Comments

  • Danny
    Danny almost 2 years

    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?

  • Danny
    Danny over 13 years
    Thanks, that makes a lot of sense. I'll try that code when I get home. Out of curiosity, how did you come to know about the "MyFileConnection" class? It seems that's my missing link!
  • Danny
    Danny over 13 years
    That WAS my missing link, rather.
  • Danny
    Danny over 13 years
    Thanks, this is useful. I did not know there were lightweight file-based database libraries available on the web. If you feel like sending me a few references or words to search for, I'd appreciate it.