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="">
    <Arg></Arg> <- Global JVM
     <New class="org.apache.commons.dbcp.BasicDataSource">
                  <Set name="driverClassName">org.postgresql.Driver</Set>
          <Set name="url">jdbc:postgresql://</Set>
            <Set name="username">me_username</Set>
            <Set name="password">me_password</Set>
            <Set name="minIdle">1</Set>

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.

   <description>DB Connection</description>

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