On Mon, 28 Oct 2002, Jacob Kjome wrote:
> Date: Mon, 28 Oct 2002 08:08:09 -0600
> From: Jacob Kjome <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: DBCP speed of lookup -vs- stored reference to Datasource?
>
>
> I'm wondering what kind of performance penalty there is, if any, when doing
> a full lookup of the Datasource object each and every time through JNDI
> calls? Basically, does it make sense to do one lookup and store a local
> copy for future use? The one problem I see with doing that is that if one
> ever wants to use the Tomcat Manager app to modify configuration on the
> fly, using the local copy of the Datasource would make it so you never
> really realize that a change to configuration has been made. Am I simply
> losing flexibility without gaining any performance?
The JNDI lookup, after some manipulations of the name, turns into a
HashMap lookup inside the server. It's not particularly expensive -- and
isn't even worth looking at (from a performance perspective) unless you've
already tuned all your database queries for maximum performance. That's
where the vast majority of problems occur.
Performance aside, there are two functionality reasons to do the lookup
every time:
* Keeping a reference to the data source yourself means that
you need to either pass it on to every method, or make it
available some other way. The lookup code requires zero references
to things like the ServletContext or any static variables.
* In a high-available application server environment (i.e. Tomcat
by itself doesn't support this), doing the lookup every time gives
the server an opportunity to gracefully deal with things like
switching to a backup database, or dynamically reconfiguring the
connection pool by giving you a new instance from now on.
>
> The way I have the lookup performed is the following. Please let me know
> if this is totally unnecessary....
>
> final class MyDataSourceClass {
>
> private static DataSource ds = null;
> public static Boolean DS_INITIALIZED = Boolean.FALSE;
>
> public static Connection getConnection() throws SQLException {
> if (DS_INITIALIZED.equals(Boolean.FALSE) || ds == null) {
> synchronized (DS_INITIALIZED) {
> if (DS_INITIALIZED.equals(Boolean.FALSE) || ds == null) {
> try {
> Context ctx = new InitialContext();
> ds =
> (DataSource)ctx.lookup("java:comp/env/jdbc/myDB");
> if (ds == null) throw new SQLException("No
> DataSource available for Connection");
> DS_INITIALIZED = Boolean.TRUE;
> }
> catch (NamingException ne) {
> throw new SQLException("JNDI Lookup Failed: " +
> ne.getMessage());
> }
> }
> }
> }
> return ds.getConnection();
> }
>
> public static void returnConnection(Connection conn) throws SQLException {
> conn.close();
> }
>
> }
>
> So, basically I want to know if the lookup isn't expensive enough to bother
> with storing the DataSource locally.
>
As others will undoubtedly point out, the "Double-Checked Locking"
algorithm you use above is not guaranteed to work. There was a JavaWorld
article on this topic last year.
> thanks,
>
> Jake
Craig
--
To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>