• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to use JNDI Connection pooling in Struts2

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to build a simple Struts2 app that uses a mySql connection pool to render a list of employees from a mySql DB on a Tomcat 7 server. I'm able to use connection pooling in a standard MVC java application, but I'm not clear of how to use JNDI lookups in Struts2. When I call the struts action (Employee_list.action), I get the following error. I tried to include my main class files. I'd really appreciate any guidance on how to correctly use JNDI for connection pooling and please let me know if I need to provide any further information. Thanks in advance!

stack trace:


ConPool.java


EmployeeDAO.java



EmployeeAction.java:



struts.xml



context.xml:



web.xml
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JNDI works in Struts the same way it works with every other Java application. You don't need to do the bind step. The context.xml file already binds the name to the resource.
The name you want to lookup in the InitialContext is the context file ("jdbc/myTest"), not the URL for the database.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe,

I really appreciate your explanation. I'm now treating the JNDI part as I would any standard MVC application. I edited the files as above. I deeted the ctx bind line in the ConPool class. The Context lookup is now set to lookup (jdbc/myTest). I modified the context.xml to take care of the DB url and name this resource as "jdbc/myTest. I also modified web.xml files. Unfortunately I'm still getting the same error except error, except now the NameNotFoundException refers to jdbc/myTest. Would appreciate knowing where I"m still going wrong? Many thanks
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are looking at the wrong place in the JNDI tree. Your code says:


Have a look at this example:

"comp" is the root of the JNDI tree and "env" are the settings you've configured in the context.xml file. Then your data source instance is located under the name you specified.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for making this so crystal clear! I modified my code as you suggested and it works fine. I just want to make sure I understand the concept here.

"comp" is the root of the JNDI tree and "env" are the settings you've configured in the context.xml file. Then your data source instance is located under the name you specified.



This would mean in a typical JNDI setting, I would always start any lookup by setting the root of the JNDI tree (ie: specifiying the env settings by extending the Context class). Correct?

In my modified code I did have to change the Datasource libraries from org.apache.tomcat.jdbc.pool.DataSource to org.apache.tomcat.dbcp.dbcp.BasicDataSource. When I used the pool.DataSource, I got:

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

Is this because of a conflict in the class libraries? Can you tell me the difference between using the two libraries or should I post seperately about that topic? If I did want to use DataSource, is there a workaround such as explicit definition of the Datasource library?



Thanks
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This would mean in a typical JNDI setting, I would always start any lookup by setting the root of the JNDI tree (ie: specifiying the env settings by extending the Context class). Correct?



You have to look up a JNDI resource using its fully qualified name. You are basically traversing a tree. I'm not sure why you would extend the Context class.

Can you tell me the difference between using the two libraries or should I post seperately about that topic?


DBCP is the default connection pool used by Tomcat. I don't know why they have a similar class in the Tomcat code base.
Why aren't you using the type you configured in context.xml?

You shouldn't care what Tomcat is using to implement its pool. Stick to the classes in the Java API. That way if/when Tomcat changes their code to use a different pool, your code doesn't have to change.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic