Wednesday, December 29, 2010

Hibernate - Reading a datasource from a jndi

I was writing this utility that read the data produced by the main application, but presented it in a different way - basically sliced and diced the information such that it was more meaningful to the user.
My goal for this utility was simple - provide as little configurations as possible - make it work more like a plug-and-play utility.
To achieve this, the first goal was to read the same jndi’s the main application was reading to fetch data. I didnt want the user to have to re-enter the connection params. Fairly simple request right?
Here’s what i had to do to achieve this:
Step1 - The JNDI was already defined - in the tomcat server.xml in this case. So make a note of the jndi name
   
      
 auth=”Container”
        type=”javax.sql.DataSource”
….
Step2 - Update your Spring application context to include the datasource bean as follows,
 
          class=”org.springframework.jndi.JndiObjectFactoryBean”>
      /jdbc/unity”/>  
     
   

Step2 - Last step - update your Context.xml (if you dont have one, create one under META-INF)
  
         jdbc/unity”
                       type=”javax.sql.DataSource”
                       global=”jdbc/unity”/>
  


Thats it!

No comments:

Search This Blog