HowTo configure a JNDI data source for lift and jetty
From Lift
In the following example of configuring lift and Jetty to use a JNDI data source, we'll assume the data source is a MySQL database. It shouldn't be too hard, however, to adapt it to other sources. (These Jetty data source examples are a good place to start.) Note that it's possible -- and simpler -- to hardcode data source connection information in Boot.scala. See HowTo configure lift with MySQL for instructions.
First, if you haven't already, make sure you have some recent version of the mysql-connector JAR. The easiest way to do this is to add a dependency like the following to your pom.xml file and let Maven obtain it for you:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency>
Jetty requires two additional JAR files to use JNDI data sources. (See Jetty's JNDI page for details.) We'll again let Maven obtain these for us by simply adding two new dependencies to our pom.xml file, changing version numbers as necessary:
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-plus</artifactId> <version>6.1.6</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-naming</artifactId> <version>6.1.6</version> </dependency>
(In fact, you may wish to add a new property to the pom.xml file, e.g. <jetty.version>[6.1.6,)</jetty.version> and then substitute ${jetty.version} for the version numbers above, as well as anywhere else the Jetty version is required.)
Now you'll want to tell Jetty that your data source is a MySQL database and also tell Jetty how to connect to it. To do this, create the file jetty-env.xml in your WEB-INF directory with contents along these lines:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure class="org.mortbay.jetty.webapp.WebAppContext"> <New id="MyDataSource" class="org.mortbay.jetty.plus.naming.Resource"> <Arg>jdbc/MyDataSource</Arg> <Arg> <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> <Set name="Url">jdbc:mysql://localhost:3306/myDatabase</Set> <Set name="User">myUsername</Set> <Set name="Password">myPassword</Set> </New> </Arg> </New> </Configure>
(Replacing myDatabase, myUsername, and myPassword with the strings specific to your database and giving MyDataSource another name if you like.)
You also have to register your new data source in WEB-INF/web.xml. Add the following lines to the main <webapp>...</webapp> section of this file:
<resource-ref> <description>This is the true source of enlightenment.</description> <res-ref-name>jdbc/MyDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
The final step in setting up the data source is to tell lift about it. Do this by adding a single line near the very beginning of Boot.scala:
DefaultConnectionIdentifier.jndiName = "jdbc/MyDataSource"
This line should be executed before, say, the one that looks something like if (!DB.jndiJdbcConnAvailable_?) DB.defineConnectionManager(DefaultConnectionIdentifier, DBVendor).
And that's it.

