On Tomcat 6.x, Hibernate 3.x, MySQL, and JNDI 27

Posted by Hisham Sun, 06 Jan 2008 00:12:00 GMT

After lots of googling and documentation reading, I got a setup of mine to work properly with Tomcat 6.x, Hibernate 3.x, MySQL, and JNDI datasources. For what its worth, most of the resources on the web regarding setting this up with the versions I specified are worthless. To save myself the future hassle, and the lot of you who might be having a hard time setting this up, here are the configuration files and directives required.
yourapp/web/META-INF/context.xml:
  <Context>
  <Resource name="jdbc/my_app" 
            global="jdbc/my_app"
            auth="Container"
            type="javax.sql.DataSource" 
            username="user"
            password="XXXX"
            driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/my_db_name?autoReconnect=true"
            maxActive="8" 
            maxIdle="4"/>
</Context>

yourapp/web/WEB-INF/web.xml:

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

hibernate.cfg.xml:

...
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.datasource">java:comp/env/jdbc/my_app</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <mappings here>    
  </session-factory>  
</hibernate-configuration>
...

You can get your session factory as usual:

sessionFactory = new Configuration().configure().buildSessionFactory();
or whichever way you prefer.
Comments

Leave a response

  1. Avatar
    Ashoka 2 months later:
    Awesome stuff !!! I spent half a day trying to fix this issue. Very helpful post indeed.
  2. Avatar
    Scot 6 months later:
    I was completely stuck on the 'java:comp/env/jdbc/my_app' portion in the hibernate config. The Apache examples all put this in the Java code as a Context lookup and it is still missing the jdbc portion. Their example: DataSource ds = (DataSource) initCtx.lookup("java:comp/env/my-datasource") Keep on coding!!!
  3. Avatar
    Ravi 9 months later:
    Really, gr8 help on hand stretch. Keep it up, Hisham
  4. Avatar
    John 9 months later:
    Thank you for the info. Excellent job!
  5. Avatar
    Arun 10 months later:
    Thank you VERY much! I've been hitting my head against the wall w/ this issue for the better part of a day - your post was exactly on point.
  6. Avatar
    Pri 11 months later:
    Thanks a lot for documenting this config. I was stuck for two days and finally your post got my project to work.
  7. Avatar
    Neeti about 1 year later:
    Thanks a lot.
  8. Avatar
    Umar about 1 year later:
    Thanks for the info. After a lot of Googling I was able to find the very info I needed.
  9. Avatar
    FIA about 1 year later:
    Short, concise, and above all correct! You've just saved me from another day of hair pulling. Cheers. :)
  10. Avatar
    Peter about 1 year later:
    Thanks a lot. After a lot of attempts and googling I finally found the info I needed.
  11. Avatar
    Dikshit over 2 years later:
    Thanks a lot for the code
  12. Avatar
    Christian over 2 years later:
    Works! Thnx!!
  13. Avatar
    Sandeep over 2 years later:
    Thanks a lot man...
  14. Avatar
    sriram over 2 years later:
    great work ..really helpful..
  15. Avatar
    Alex over 2 years later:
    Thank you very much. It helped to do my job in time
  16. Avatar
    Sumved Shami over 2 years later:
    Thanks a lot. You have saved hell lot of our time. We appreciate that. :-)
  17. Avatar
    Sumved Shami over 2 years later:
    Instead of writing JNDI config in application's context.xml file, cant we write this in application server context file? Please explain.
  18. Avatar
    Ann over 2 years later:
    Thanks for information!
  19. Avatar
    JayM over 2 years later:
    thx so much for this config!!!! it really saved my day!!!! THANK YOU!!!!!!!!!!!!!!!!
  20. Avatar
    server.xml over 2 years later:
    Tomcat 6 documentation: "Tomcat 6 maintains a separate namespace of global resources for the entire server. These are configured in the element of $CATALINA_BASE/conf/server.xml. You may expose these resources to web applications by using a to include it in the per-web-application context."

    => Define the resource in server.xml section, then expose it by using a in in the context.xml file.
  21. Avatar
    server.xml over 2 years later:
    Edit: Tomcat 6 maintains a separate namespace of global resources for the entire server. These are configured in the <GlobalNamingResources> element of $CATALINA_BASE/conf/server.xml. You may expose these resources to web applications by using a <ResourceLink> to include it in the per-web-application context. (http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html)
  22. Avatar
    Dileep (dilseti@gmail.com) over 3 years later:
    Hi Thanks a lot I was completely stuck on the 'java:comp/env/jdbc/my_app' portion in the hibernate config. The Apache examples all put this in the Java code as a Context lookup and it is still missing the jdbc portion. Keep rocking!
  23. Avatar
    Mens FiveFingers Bikila over 3 years later:
    if you want become more beautiful,you should san this website. Mens FiveFingers Bikila
  24. Avatar
    Hosein over 3 years later:
    Indeed you are the best maaaan thank you
  25. Avatar
    Yatin Panjabi over 3 years later:
    Your blog has solved my problem i have been searching this type of help and you have done extra ordinary work Thank you very much for your kind and worthy help
  26. Avatar
    Marcuz over 3 years later:
    Great useful stuff... been searching for half a day to get it work, and with your tips, it takes just few seconds to set it up correctly.
  27. Avatar
    Manikandan over 4 years later:
    Its solved my problem with in 5 Mins. Really wonderful.... good work....
Comments