RSS
 

Jetty Jndi setup with postgresql

07 Dec

So i was trying to get one of my web apps to run under jetty and was having problems with the global jvm jndi context. This is really simple, however it took me some time to figure it out. So i am posting my notes here, because with me i do something then a year later i will need to do it again and forget how. So here is how i got it going. Now this is with Jetty 6 i have not used Jetty 7 yet. I am using the dbcp from apache with postgresql database. You will need to download 3 files from the net.

  1. Postgresql jdbc driver
  2. Apache commons-dbcp
  3. Apache commons-pool

Once you have these files you will want to save them in the jetty-version/lib/ext directory. Now we can configure jetty with our jndi connection.

Open your jetty.xml file and add the following somewhere between the starting <Configure id="Server" class="org.mortbay.jetty.Server"> and the closing </Configure>

<New id="GlobalConnection" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg></Arg> <- Global JVM
    <Arg>jdbc/pg</Arg>
    <Arg>
     <New class="org.apache.commons.dbcp.BasicDataSource">
                  <Set name="driverClassName">org.postgresql.Driver</Set>
          <Set name="url">jdbc:postgresql://127.0.0.1:5432/me_db</Set>
            <Set name="username">me_username</Set>
            <Set name="password">me_password</Set>
            <Set name="minIdle">1</Set>
     </New>
    </Arg>
   </New>

Save that file then in your projects WEB-INF/web.xml file you will want to add the following before the closing </web-app> tag.

<resource-ref>
   <description>DB Connection</description>
   <res-ref-name>jdbc/pg</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

Now you can save this file fire up jetty and you should be able to get your jndi name and connect to the database. If you have issues let me know.

 

Leave a Reply

Comments are closed