On Tue, Mar 4, 2014 at 8:57 PM, Neven Cvetkovic <neven.cvetko...@gmail.com>wrote:
> On Tue, Mar 4, 2014 at 2:56 PM, Charles Richard < > charle...@thelearningbar.com> wrote: > > > On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz < > > ch...@christopherschultz.net> wrote: > > > Can we still use Hibernate in our Spring application if we > > > > configure the Data Source through a context.xml? > > > > > > Yup. You just need to tell Hibernate that you already have a > > > DataSource. I'm not exactly sure how to do that, but I'm confident it > > > can be done. You posted only the DataSource configuration itself and > > > it didn't have a name (other than the "id"). You'll need to figure out > > > how to get Hibernate to use an existing (external) DataSource rather > > > than configuring it yourself as you have done. > > > > > > I'm really not sure how that could be done either but hopefully the > > developers here could help me with this. > > > > > Hi Neven, > Charles, > > As Chris pointed out - there are two ways you can get a datasource into > your Spring application: > > 1) Spring managed resource (datasource) - e.g. C3P0, Tomcat Pool > Datasource, etc... this is the one you've been using. Connection pool is > implemented and managed by the Spring container. That usually doesn't > register itself with JMX MBeanServer. > > You define datasource in the Spring configuration, e.g. > > <bean id="myDatasource" ... > > ... > </bean> > > > 2) Tomcat / container managed resource (datasource) - defined by the > <Resource> in the Tomcat configuration. > > See more details on JNDI entries here > https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html > > You will notice there are two ways to configure your resource in Tomcat > container: > > (a) a global resource, so every deployed application will have access to > the same entry, as defined in TOMCAT_DIR/conf/server.xml > (b) per application resource, as defined in > YOURAPP.war/META-INF/context.xml (as Chris suggested). > > I'm trying to get option (b) to work and I'm getting this error: Cannot create JDBC driver of class '' for connect URL 'null' java.lang.NullPointerException It also seems like Tomcat is not picking up my context.xml. Any suggestions? My context.xml in /ourpath/META-INF: <?xml version="1.0" encoding="UTF-8"?> <Context antiJARLocking="true" path=""> <Resource name="jdbc/ourtest" auth="Container" type="javax.sql.DataSource" initialSize="0" maxActive="100" maxIdle="20" maxWait="10000" mindIdle="0" name="xxx" password="xxxxxxxxxxxx" removeAbandoned="true" removeAbandonedTimeout="1800" url="jdbc:mysql://xxx.xxx.xxx.xxx/jdbc_testing" /> </Context> My hibernate-context.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> .... <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ourtest" /> Relevant section in web.xml: <resource-ref> <description>TLB Datasource</description> <res-ref-name>jdbc/ourtest</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> There are pros and cons for both approaches. > > Once you register your resource with JNDI, you can reference that > datasource in your spring configuration with: > > <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/> > > (works in newer Spring 3.x, and possibly 2.x) > > You can see more details here: > > http://docs.spring.io/spring/docs/3.0.x/reference/xsd-config.html#xsd-config-body-schemas-jee > > http://docs.spring.io/spring/docs/2.5.6/reference/xsd-config.html#xsd-config-body-schemas-jee > > Hopefully, that answers your question. > > Cheers! > Neven > Thanks, Charles