Permalink
Browse files

Described JNDI init for Squeryl+Record

  • Loading branch information...
1 parent f2d8a38 commit c31653f3075a0e1b5912a57c8514614c420e005c @d6y committed May 6, 2012
Showing with 51 additions and 0 deletions.
  1. +51 −0 docs/0400-SQL-Record-Squeryl/0100-JNDI.md
@@ -0,0 +1,51 @@
+Using a JNDI datasource
+===========
+
+Problem
+-------
+
+You want to use a JNDI data source for your Record+Squeryl Lift application.
+
+Solution
+--------
+
+In `Boot.scala` call `initWithSquerylSession`...
+
+```scala
+import javax.sql.DataSource
+val ds = new InitialContext().
+ lookup("java:comp/env/jdbc/mydb").asInstanceOf[DataSource]
+
+SquerylRecord.initWithSquerylSession(
+ Session.create(ds.getConnection(), new MySQLAdapter) )
+```
+
+...replacing `mydb` with the name given to your database in your JNDI configuration, and replacing `MySQLAdapter` with the appropriate adapter for the database you are using.
+
+
+Discussion
+----------
+
+The Java Naming and Directory Interface (JNDI) is service provided by the web container (e.g., Jetty, Tomcat, etc) which allows you to configure a database connection in the container and then refer the connection by name in your application. One advantage of this is that you can avoid including database credentials to your Lift source base.
+
+The configuration of JNDI is different for each container, and may vary with versions of the container you use. The _See Also_ section includes links to the documentation pages for popular containers.
+
+Some environments may also require that you to mention the JNDI resource in your `src/main/webapp/WEB-INF/web.xml` file:
+
+```xml
+<resource-ref>
+ <res-ref-name>jdbc/mydb</res-ref-name>
+ <res-type>javax.sql.DataSource</res-type>
+ <res-auth>Container</res-auth>
+</resource-ref>
+```
+
+See Also
+--------
+
+* List of [Squeryl database adapters](http://squeryl.org/api/org/squeryl/adapters/package.html).
+* An example [Jetty JNDI configuration](http://www.assembla.com/spaces/liftweb/wiki/Apache_and_Jetty_Configuration) on the Lift wiki.
+* [Jetty 6 JNDI documentation](http://docs.codehaus.org/display/JETTY/JNDI).
+* Eclipse [Jetty JNDI documentation](http://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource).
+* [Tomcat 7](http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html) JNDI information.
+

0 comments on commit c31653f

Please sign in to comment.