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 exam开发者_如何学Cple of what the code inside getConnection() should look like?
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:
- 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)
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.
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...
精彩评论