aspose file tools*
The moose likes JDBC and the fly likes Datasource lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Datasource lookup" Watch "Datasource lookup" New topic
Author

Datasource lookup

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Hi, i am using Tomcat 4.1.31 to run a simple web application. I am looking for advice on coding my DAOs. For example, when I access a DAO to retrieve a single column value, the first thing in my code is:

try {
context = new InitialContext();
datasource = (DataSource) context.lookup("java:comp/env/jdbc/testPortletDB");
} catch (NamingException ne) {
LOG.debug("Unable to lookup datasource. " + ne.getMessage());
}

try {
conn = datasource.getConnection();
} catch (SQLException sqlex) {
LOG.debug("Unable to open database connection. " + sqlex.getMessage());
}


The above code works fine but I am suspecting that I should not be looking up the datasource each time the DAO is called. Should the above code be placed in the constructor? Or should I have a static method that would return a reference to the datasource?
Any advice would be great. Thanks.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Andy Hahn:

I am suspecting that I should not be looking up the datasource each time the DAO is called. Should the above code be placed in the constructor? Or should I have a static method that would return a reference to the datasource?



good thought !! A example for need of ServiceLocator & caching Pattern.

Use ServiceLocator Pattern, and cache the look-ups in a hashmap of Servicelocator class.
It will not only saperate your look up code but other code can also re-use same lookup in other class

Shailesh
[ April 10, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
That looks like the perfect solution. Thanks!!
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Question:
What advantage does caching provide? I have used the Service Locator class to ensure the datasource is looked up only once. Should each request for a connection generate a new connection or would I want to reuse a single connection? I would think I would just return a new connection for each request.

Thanks.


public static ServiceLocator getInstance() {
return instance;
}

private ServiceLocator() {
try {
context = new InitialContext();
datasource = (DataSource) context.lookup(datasourcename);
} catch (NamingException ne) {
LOG.debug("Unable to lookup datasource. " + ne.getMessage());
}
}

public Connection getDBConnection() {
try {
conn = datasource.getConnection();
} catch (SQLException sqlex) {
LOG.debug("Unable to open a database connection. " + sqlex.getMessage());
}

return conn;
}
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Andy Hahn:

What advantage does caching provide?


In SeriveLocator we cache the look up datasource,hence it removes overhead of looking up every time,becauase lookups are considered little costly in comparision to using same reference.


Should each request for a connection generate a new connection or would I want to reuse a single connection? I would think I would just return a new connection for each request.


Here you are using connection from DataSource, and connection management would be done by DataSource.
It depends on datasource if it return same connection or creates new conenction.You need not to worry as datasource will handle based on connection pool parameter you code if perfectly good.

By using DataSource we get advantage of connection pooling and distributed transactions. Connection pooling increases performance by reusing connections rather than creating a new physical connection



Shailesh
[ April 10, 2005: Message edited by: Shailesh Chandra ]
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Let me try to explain my questions a little differently.
1. What advantage does caching provide?
- The concept of why to cache makes sense.
- I was deciding whether to use a static Datasource reference to store the datasource OR a HashMap reference. Would it be necessary to use a HashMap if all the service is returning is a single Datasource?

2. Should each request for a connection generate a new connection or would I want to reuse a single connection? I would think I would just return a new connection for each request.
- I was wondering if it would be ok to use the following every time a connection is requested:
conn = datasource.getConnection();

- Or would I cache conn (above) and return that reference for every request for a connection.

Thanks.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Andy Hahn:

I was deciding whether to use a static Datasource reference to store the datasource OR a HashMap reference. Would it be necessary to use a HashMap if all the service is returning is a single Datasource?



If you are retruning only datasource then only a static Datasource referece would suffice.The use of HashMap is not mandory.

ServiceLocator is not only used for DataSource Lookup but also used for other resourse look up like ejb home etc etc or some environment paramter value. Then It would not be a good idea creating a stating variable for every resource.
That is why we use hashmap where we put our look up against a key.

Here is code snippend of my ServiceLocator



here I am using service locator for various look up looking ejb home, environment parameter etc etc


I cache conn (above) and return that reference for every request for a connection.


I would recommend not to do this, because if one code closes conection then other code will get a closed connection and this would cause an error.
Also this will cause only one connection to be used for all and you will not be able to take advantage of connection pool


Shailesh
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Got it. Works great now. Thanks!
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761


It depends on datasource if it return same connection or creates new conenction.You need not to worry as datasource will handle based on connection pool parameter you code if perfectly good.

What if I wanted a new connection everytime ? How would I configure this in say e.g. JBoss ?

Pho
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Pho Tek:

What if I wanted a new connection everytime ? How would I configure this in say e.g. JBoss ?




What advantage do you want to achieve by this ?
 
Don't get me started about those stupid light bulbs.
 
subject: Datasource lookup
 
Similar Threads
JNDI name lookup
What Exception if database down
ServeltContextListener for DataSource
Hibernate Tool custom template
How to test persistence layer of a web based application?