3

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?

3
  • 1
    I think there's some confusion going here. What's after all your functional requirement? Just connecting a specific DB using a specific JDBC driver by a datasource? Or do you really want to homegrow your own JDBC driver as your question is currently insinuating? Commented Nov 2, 2010 at 7:57
  • 1
    Always refer the latest docs so that you don't end up picking up any "phased out" advice; download.oracle.com/javase/6/docs/technotes/guides/jdbc/… Commented Nov 2, 2010 at 8:06
  • javax.sql.DataSource is an interface which defines that method. It's driver vendor's job to implement the method. In case you plan to write your own driver (for whatever reason), you would have to implement the interface. Code inside getConnection would depend on the underlying "database" to which it establishes connection. Commented Nov 2, 2010 at 8:51

3 Answers 3

3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

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!
That WAS my missing link, rather.
2

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.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.