wood burning stoves*
The moose likes Object Relational Mapping and the fly likes Shiro Hibernate Tomcat - how to unify datasources using JNDI from Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Shiro Hibernate Tomcat - how to unify datasources using JNDI from Hibernate" Watch "Shiro Hibernate Tomcat - how to unify datasources using JNDI from Hibernate" New topic
Author

Shiro Hibernate Tomcat - how to unify datasources using JNDI from Hibernate

Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
Questions are marked bold below. This is the context of the question:

Shiro wants a JNDI datasource:


so I configured it one in Tomcat:


however, I'm using another setup from hibernate for my application:


This is also necessary in web.xml for the line in hibernate.cfg.xml:


I think I'm using two separate datasources, aren't I?Is that bad? Because I can't tell wether I'm using one PooledDataSource or two at the moment... Everything looks fine, except I think I will run into connection problems or the datasource registered in JNDI because it's not properly set with c3p0... also need to confirm this is a proper setup for this scenario.

What I want, is to register the hibernate datasource in JNDI to use it with Shiro, in common with the rest of my application. How do I do that? Is there any chance the configuration I'm using treats both datasources with the same c3p0 configuration, or maybe it sees the two datasources as one? Hopeful thinking...

Logs indicate there's two separate data sourcepools registering:
One at start, immediately after "INFO: Initializing Shiro environment", probably the tomcat configured one:


and the other later(s!?) when application calls on it, probably the hibernate one:


but afterwards there's a lot of pools initializing:

This is from the datasource configured in tomcat


this seems related to the initial settings of c3p0 in hibernate.cfg.xml


then on a short distance this appears:

... probably a separate thread and connection/settings for caching statements? hmm...

then another initialization


and afterwards, when there's idle checks, only one triggers according to settings:


meaning the other one will run into problems after the 8 hours set by mysql wait_timeout of 28800 seconds (in fact that's exactly what happened if I think about it, because authorization/authentication failed ...can't tell if the application crapped out too because everything was behind Shiro, anyway, enabling c3p0 for the hibernate connection will not enable it for Shiro datasource too...)

EDIT: In the meantime I discovered this, for Tomcat 7 (lucky me if it works):
http://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html

EDIT2: EDIT doesn't work, there's some bug going on that was patched supposedly but can't get it to work.
Anyway, that's for a JDBC pooled datasource. For a com.mchange.v2.c3p0.ComboPooledDataSource there's
http://www.mchange.com/projects/c3p0/

still, it would be nice if I could register the hibernate datasource in JNDI...
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
There's hibernate.jndi property to be set but I haven't the time to get into that now... dunno if that's the answer.
For some reason the other people that asked the same question didn't make use of this option.
 
Consider Paul's rocket mass heater.
 
subject: Shiro Hibernate Tomcat - how to unify datasources using JNDI from Hibernate