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
« CDI tutorial links
Finishing up the CDI TCK »

CanDI for database integration

In the large projects that most of us work with, it’s not feasible to rewrite the entire project around a new technology like CDI, even though a redesigned project around CDI would be a better crafted solution. But what you can do is add CDI incrementally and improve your project’s quality piece by piece as time goes on. For example, in an agile-style short release cycle, you’d want to break up the CDI migration task into something small enough to fit in your 2-3 week cycle.

If you’re using JDBC directly, the quickest benefit for CDI is replacing your current DataSource injection (or JNDI) with CDI, since Resin’s database configuration integrates with the CDI injection.

First, you need to decide on the qualifiers for the database, a tiny set of adjectives describing each DataSource that you use by its functionality. If you have only one DataSource, you can use the builtin @Default qualifier. If you have two or more, you’ll create your own qualifiers. For example, you might have a @Login database separate from your @Account database. When you inject them, it’ll look like:

import javax.inject.Inject;
import javax.sql.DataSource;

public class MyServlet extends GenericServlet {
@Inject @Account DataSource _accountDataSource;
…
}

The @Account qualifier is simple boilerplate code, which looks like the following:

package com.mycom.mypkg;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

@Target({TYPE,FIELD,METHOD,PARAMETER})
@Retention(RUNTIME)
@Qualifier
public @interface Account {
}

We’re almost done. The only thing left is to modify the <database> configuration in the resin-web.xml to attach the @Account qualifier to the <database>.

<web-app xmlns=”http://caucho.com/ns/resin”
xmlns:mypkg=”urn:java:com.mycom.mypkg”>

<database jndi-name=”jdbc/account”>
<mypkg:Account/>
<driver type=”com.mysql.jdbc.Driver”>
<url>jdbc:mysql://localhost:3306/account</url>
<user>myuser</user>
<password>mypassword</password>
</driver>
</database>

</web-app>

The <mypkg:Account/> adds the @Account qualifier to the Resin <database> configuration, making it available for the CanDI injection in your code.

So with a simple change to your code and configuration, your DataSource injection is now type-safe, validated at application startup time by Resin, and self-documenting the source of the injection at the injection point with @Inject @Account DataSource.

This entry was posted on Monday, June 21st, 2010 at 11:04 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 ®