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.
>
>
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).

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

Reply via email to