My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Sunday, February 17, 2013

Tomcat JNDI Exception DataSource

·         DO NOT configure the DataSource (or) EntityManagerFactory in the Spring beans.
·         The version of Tomcat SHOULD BE minimum of 7.0.25. For lower versions of Tomcat you still need to use the regular connection like driverà url à password etc., This is a BUG with Tomcat server itself.
·         If you try to use DataSource with earlier versions of the Tomcat, then you will see JNDIExceptions
·         Since Tomcat does not support JTA , you need to have the following configuration for the datasource in your persistence.xml file
<persistence-unit name="<<Name>>"  transaction-type="RESOURCE_LOCAL">   
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <!--GLOBAL_JNDI_GOES_HERE This is for Tomcat which does not support JTA-->
   <non-jta-data-source>java:comp/env/jdbc/<<Your DataSource Name>></non-jta-data-source>
·         For hibernate way it will be the following
<property name="hibernate.connection.datasource">java:comp/env/jdbc/<<Your DataSource Name>></property>
·         Note that for Tomcat you need to make sure that you are reading the datasource as a “resource” and that’s why you need have the java:compo/env as a prefix for the datasource.
·         Now normally you configure the resources in either.
a.       <Tomcat_Home>\Conf\context.xml (or)
b.      <Tomcat_Home>\Conf\server.xml (or)
·         But once this is done as above the whole of Tomcat applications will have access to that resource which we do not want to do. This also means that that you need to have database specific driver files loaded and visible to all the applications deployed.
·         EPH proposes to use application specific Context files. Here are the steps for the same.
·         Create a file called “context.xml” in your webapp folder
a.       WebContent\META-INF
·         The contents of this file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/<<Your DataSource Name>>">
        <Resource name="jdbc/<<Your DataSource Name>>"
                          auth="Container"
                          type="javax.sql.DataSource"
                          driverClassName="oracle.jdbc.driver.OracleDriver"
                          url="jdbc:oracle:thin:@<<server>>:<<port>>:<<instance>>"
                          username="<<userid>>"
                          password="<<pwd>>"
                          maxActive="20"
                          maxIdle="30"
                          maxWait="-1"
        /> 
</Context>
·         Note the equality in “name” and “path” in the above file.
This way your DB driver files are loaded at runtime (WEB-INF\lib) and not at server start up time.

No comments :