main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« Resin 4.0.7
CDI tutorial links »

CDI @Produces for JDBC Connections

The combination of the CDI @Produces feature with @RequestScoped or @TransactionalScoped is an interesting patter that lets you easily manage connections by skipping the old try-finally block entirely. Like the @Pooled beans, it reduces complexity by avoiding try-finally, and more importantly makes the application more testable. In theory, to test the try-finally pattern, you need a test with a forced exception to verify that the finally actually exists, or more importantly that some future refactoring doesn’t remove the finally by mistake.

The resource proxy pattern of CDI uses the @NormalScope with @Produces. A @NormalScope like @RequestScoped or @TransactionalScoped injects a proxy to the resource, not the resource itself. For JDBC Connections, each thread gets its own actual Connection, while all threads share the same Connection proxy through injection.

Using it is trivial: just inject the Connection. In this example, I’m assuming an application @Qualifier @User, as if the application has a separate @User database distinct from other databases. As usual with CDI, if you only have a single database, you can just use the @Default qualifier.

public class MyServlet extends GenericServlet {
  @Inject @User
  private Connection _userConnection;

  void doUserLookup()
  {
     Statement stmt = connection.createStatement();
      ...
   }
}

When the connection.createStatement() executes, Resin will create a new instance of the actual connection and use it to return the statement. Any further uses of the connection in this thread will use the same connection. When the request completes, i.e. the @RequestScoped finishes, the factory will close the connection instance, returning it to the pool.

The factory for the connection is just a matched pair of a @Produces method and a @Disposes method to allocate the actual connection and free the connection. The proxy generation happens automatically because of the @RequestScoped (or any @NormalScoped) scope.

import javax.inject.*;
import javax.enterprise.inject.*;

public class MyJdbcFactory {
  @Inject @User DataSource _dataBase;

  @Produces @User @RequestScoped
  public Connection create()
    throws SQLException
  {
     return _dataBase.getConnection();
  }

  public void free(@Disposes @User Connection conn)
    throws SQLException
  {
    conn.close();
  }
}

Since Resin’s CDI itself guarantees that free will be called for any connection, it’s replacing all of our finally blocks. We’re using the @User qualifier to select the database, because the DataSource we’re injecting is also a @User database to match the @User connection.

This proxy factory pattern extends to any resource that has a create/free pattern. Refactoring multiple create/free calls into a single CDI factory simplifies the code, and more importantly improves the testability and reliability of your application.

This entry was posted on Wednesday, May 26th, 2010 at 9:53 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®