Discussion:
Can I use jmx to see jdbc pool variables/properties in Tomcat 6?
Charles Richard
2014-03-04 18:03:57 UTC
Permalink
Hi,

I am testing the jdbc pool to replace the c3p0 pool we were using for our
Tomcat connection pool. We are also using Spring 2.0 and Hibernate (and
Tomcat 6).

When I put this in my hibernate-context.xml, our application is using the
jdbc pool and appears to work:

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://${db.host}/${db.name}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.pwd}" />
<property name="initialSize" value="15" />
<property name="maxActive" value="150" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="jmxEnabled" value="true" />
<property name="jdbcInterceptors"
value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=10000)"
/>
</bean>

However, when using jmx locally, I don't see any beans that I can use to
monitor the connections used in the pool. I tried doing as per this article:

http://stackoverflow.com/questions/3865445/cant-see-jmx-entries-in-jconsole-when-using-tomcat-jdbc-connection-pool

This doesn't work for me.

Any suggestions on what I'm missing here?

Thanks,
Charles
Christopher Schultz
2014-03-04 18:57:28 UTC
Permalink
Charles,
Post by Charles Richard
Hi,
I am testing the jdbc pool to replace the c3p0 pool we were using
for our Tomcat connection pool. We are also using Spring 2.0 and
Hibernate (and Tomcat 6).
When I put this in my hibernate-context.xml, our application is
<bean id="dataSource"
class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close"> <property name="driverClassName"
value="com.mysql.jdbc.Driver" /> <property name="url"
value="jdbc:mysql://${db.host}/${db.name}" /> <property
name="username" value="${db.user}" /> <property name="password"
value="${db.pwd}" /> <property name="initialSize" value="15" />
<property name="maxActive" value="150" /> <property
name="removeAbandoned" value="true" /> <property
name="removeAbandonedTimeout" value="1800" /> <property
name="logAbandoned" value="true" /> <property name="jmxEnabled"
value="true" /> <property name="jdbcInterceptors"
value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=10000)"
/>
Post by Charles Richard
</bean>
However, when using jmx locally, I don't see any beans that I can
use to monitor the connections used in the pool. I tried doing as
http://stackoverflow.com/questions/3865445/cant-see-jmx-entries-in-jconsole-when-using-tomcat-jdbc-connection-pool
This doesn't work for me.
Any suggestions on what I'm missing here?
Tomcat version? (be specific)

If you are using Hibernate to create the DataSource, then it will not
likely be registered with the JMX server. If you use a DataSource
<Resource> configured in your webapp's context.xml, then all will be
well and you should be able to discover your DataSource via JMX. Just
tell Hibernate what the JNDI DataSource's name is and it should work fine.

- -chris
Charles Richard
2014-03-04 19:10:17 UTC
Permalink
The tomcat version is 6.0.30.

Can we still use Hibernate in our Spring application if we configure the
Data Source through a context.xml? We are not using a context.xml right
now, can I use the context.xml in $TOMCAT_HOME/conf?

Our server.xml, the config is as follows:

<Host name="www.oursite.com" appBase="/ourpath"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase=""
reloadable="true" />

Thanks,
Charles



On Tue, Mar 4, 2014 at 2:57 PM, Christopher Schultz <
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Charles,
Post by Charles Richard
Hi,
I am testing the jdbc pool to replace the c3p0 pool we were using
for our Tomcat connection pool. We are also using Spring 2.0 and
Hibernate (and Tomcat 6).
When I put this in my hibernate-context.xml, our application is
<bean id="dataSource"
class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close"> <property name="driverClassName"
value="com.mysql.jdbc.Driver" /> <property name="url"
value="jdbc:mysql://${db.host}/${db.name}" /> <property
name="username" value="${db.user}" /> <property name="password"
value="${db.pwd}" /> <property name="initialSize" value="15" />
<property name="maxActive" value="150" /> <property
name="removeAbandoned" value="true" /> <property
name="removeAbandonedTimeout" value="1800" /> <property
name="logAbandoned" value="true" /> <property name="jmxEnabled"
value="true" /> <property name="jdbcInterceptors"
value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=10000)"
/>
Post by Charles Richard
</bean>
However, when using jmx locally, I don't see any beans that I can
use to monitor the connections used in the pool. I tried doing as
http://stackoverflow.com/questions/3865445/cant-see-jmx-entries-in-jconsole-when-using-tomcat-jdbc-connection-pool
Post by Charles Richard
This doesn't work for me.
Any suggestions on what I'm missing here?
Tomcat version? (be specific)
If you are using Hibernate to create the DataSource, then it will not
likely be registered with the JMX server. If you use a DataSource
<Resource> configured in your webapp's context.xml, then all will be
well and you should be able to discover your DataSource via JMX. Just
tell Hibernate what the JNDI DataSource's name is and it should work fine.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIcBAEBCAAGBQJTFiIXAAoJEBzwKT+lPKRYbyMQAMjNMVExzKobz/IiRDJGZq7Q
uxik9/v05jmTUBuOU9eA3F/qCT/nFcObp+B9AsaUaI9FxuIQVJSV2lD6BjDSEoId
Hzkf6d2uYMy+6bGS717T/4vbZn6x02CmeSHSlJjW11C5UJjYcn/GPjv35Jf8bXE4
bnALGdMUhLxRGCCNTLgU5WycYNTaXB/5H4wZf4Q3rldMVB5oqQEQ+9j1tnroU1fC
YN+/K9sEER8EYDYjX3c10Ag1PIh35akdeJQYTGO30dEXo2zLRF7Xy1tayOkhBHfZ
e2bZsheUaTbcnC3W82kkLm+WbSZ7SKIP1GvP7GQBwisdQfEYs7XprYwYituhn0nQ
2lgWXe/f+e1HN7ySkLA9o/AL8szpU4SHUYHYeDaPn3VbkM6Wqx4lPuhIIChzHwiD
3h4Hin+cQl1JzuBvmp4pBE3cAY9sco0T6t9BTGf9U2xwjyhwQ94njyGdIRwksNKG
OIC4BpmYXzHXdss5XXU/Ci7woX0iGdOTMXy5dLACo1TOjoHBsE/y3Xg1LsZygXfi
19zZ2XRsOwnGmgXZBbM+tkuWF7uMUK/ViVkHEwfgu7IQ5QK3OZuorMTVDAgowvzm
74AV6MS3e69/TWVsALtI4mcOjVBFtz4b5pFhJgjCqGrSacoj1tAqVxi4jCA/bD0/
1XVnQ/pn5O9wsmycn0y/
=Fg28
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
Christopher Schultz
2014-03-04 19:17:52 UTC
Permalink
Charles,
Post by Charles Richard
The tomcat version is 6.0.30.
You should look at the changelog and security reports. I think you're
going to want to upgrade.
Post by Charles Richard
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.
Post by Charles Richard
We are not using a context.xml right now, can I use the
context.xml in $TOMCAT_HOME/conf?
No! That's the system-wide defaults for all web applications deployed
to the container. You want META-INF/context.xml within your WAR file
(or exploded WAR-dir).
Post by Charles Richard
<Host name="www.oursite.com" appBase="/ourpath" unpackWARs="true"
autoDeploy="true"> <Context path="" docBase="" reloadable="true"
/>
You should not have a <Context> element at all in your server.xml.
It's a wonder that your application even works with a path of "" and a
docBase of "". I'll bet you have a WEB-INF directory directly in your
webapps/ directory, right?

Sounds like you've got a mess on your hands.

Move that <Context> into META-INF/context.xml, remove the "docBase"
and "path" attributes, and then add a <Resource> as a child to
configure your DataSource. You'll also need to move your JDBC driver
from WEB-INF/lib into Tomcat's lib/ directory.

- -chris
Charles Richard
2014-03-04 19:56:14 UTC
Permalink
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Charles,
Post by Charles Richard
The tomcat version is 6.0.30.
You should look at the changelog and security reports. I think you're
going to want to upgrade.
That is something that would be the next priority after understanding our
connection pooling issues. If upgrading Tomcat would help with this as
well, we would definitely try to fasttrack it. We usually try to change one
thing at a time on our production environment.
Can we still use Hibernate in our Spring application if we
Post by Charles Richard
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.
Post by Charles Richard
We are not using a context.xml right now, can I use the
context.xml in $TOMCAT_HOME/conf?
No! That's the system-wide defaults for all web applications deployed
to the container. You want META-INF/context.xml within your WAR file
(or exploded WAR-dir).
Post by Charles Richard
<Host name="www.oursite.com" appBase="/ourpath" unpackWARs="true"
autoDeploy="true"> <Context path="" docBase="" reloadable="true"
/>
You should not have a <Context> element at all in your server.xml.
It's a wonder that your application even works with a path of "" and a
docBase of "". I'll bet you have a WEB-INF directory directly in your
webapps/ directory, right?
We've got a WEB-INF folder in our appBase folder reference
(/ourpath/WEB-INF).
Sounds like you've got a mess on your hands.
I guess.
Move that <Context> into META-INF/context.xml, remove the "docBase"
and "path" attributes, and then add a <Resource> as a child to
configure your DataSource. You'll also need to move your JDBC driver
from WEB-INF/lib into Tomcat's lib/ directory.
In our /ourpath/META-INF, there is no context.xml, just a MANIFEST.MF
file. I can create one if that's what I'm supposed to do.

Thanks for the help and my apologies for not replying inline on the first
reply!
- -chris
Cheers,
Charles
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIcBAEBCAAGBQJTFibgAAoJEBzwKT+lPKRYyzQP/RNazj5VESUpXcPqblaGV0Yt
PD7s3BmTNkYhrkKk2MMCIvLThySKtVUsKxEH6UfsCpKoJCUUR8mfmLCd+edTwIFK
EEm/4XfaxNcY4wh0OcVPKO7onVaJJ012rFkh74s8sGfThr9+wiyiBc43+vs9YwJ7
LpRBLxNuQp2d8HpMAqc87k1AS6A/nYdL8rCfOBQxDZ9c3/8e957P0+5Lf86u/ePj
1ckPrh/LUX3fy+JDaNOyGK2+9jg2E5g+QsZt/x4Wd6p30UnPdYDHd9pxibmyxOhb
Guj5Y8ZW5+4OfQ5l2oGGYzyawIiM5HXgdB6FoRZGOG3SvEwWtI+uUBOGRR8oAGTY
Bjm5/I0Y6gC9m2g24nAo5VSJu4cMvj7icsT557xGcIMIQyoAFaLziG8IyVzTlTjV
IlCijFxfMFzv/imMMQFcKbLSD6LfyRqQtxPbK+K0CKYGkZnsVasL+24RDr5L61SK
of/2WSR4rMnd+wcMR9LZJXJhR3X8I8MSdCk1535WssG3kjvQWqD7nHYCjRcWrdXs
pXMZaoNFcoby+fkl1omqF9XLUC3Bi4sslJPopnef+pvyzroY/jxznJqas8HnMWyK
RtIrDsWF+aU5k5o+y1SikOl4pZgQ/abwD4r0eGiSqD0vdnHpiX4MOg3UL1so+yQ5
we5YkJL9UklDrYT7mARZ
=6gLB
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
Christopher Schultz
2014-03-04 20:05:47 UTC
Permalink
Charles,
Post by Charles Richard
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
Charles,
Post by Charles Richard
Post by Charles Richard
The tomcat version is 6.0.30.
You should look at the changelog and security reports. I think
you're going to want to upgrade.
Post by Charles Richard
That is something that would be the next priority after
understanding our connection pooling issues. If upgrading Tomcat
would help with this as well, we would definitely try to
fasttrack it. We usually try to change one thing at a time on our
production environment.
A very good policy. No, I don't think this will help you with your
DataSource stuff. But you will want to get this into testing ASAP.
Honestly, you should probably be looking at Tomcat 7 or even Tomcat 8
for getting into your development and/or testing environments.
Post by Charles Richard
Post by Charles Richard
Post by Charles Richard
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.
Post by Charles Richard
I'm really not sure how that could be done either but hopefully
the developers here could help me with this.
They should be able to do that.
Post by Charles Richard
Post by Charles Richard
Post by Charles Richard
We are not using a context.xml right now, can I use the
context.xml in $TOMCAT_HOME/conf?
No! That's the system-wide defaults for all web applications
deployed to the container. You want META-INF/context.xml within
your WAR file (or exploded WAR-dir).
Post by Charles Richard
Post by Charles Richard
<Host name="www.oursite.com" appBase="/ourpath"
unpackWARs="true" autoDeploy="true"> <Context path=""
docBase="" reloadable="true" />
You should not have a <Context> element at all in your server.xml.
It's a wonder that your application even works with a path of ""
and a docBase of "". I'll bet you have a WEB-INF directory directly
in your webapps/ directory, right?
We've got a WEB-INF folder in our appBase folder reference
(/ourpath/WEB-INF).
Sounds like you've got a mess on your hands.
Post by Charles Richard
I guess.
Move that <Context> into META-INF/context.xml, remove the
"docBase" and "path" attributes, and then add a <Resource> as a
child to configure your DataSource. You'll also need to move your
JDBC driver from WEB-INF/lib into Tomcat's lib/ directory.
Post by Charles Richard
In our /ourpath/META-INF, there is no context.xml, just a
MANIFEST.MF file. I can create one if that's what I'm supposed to
do.
Yep, just create a new one.

- -chris
Mark Eggers
2014-03-04 23:00:46 UTC
Permalink
Since it's only two lines, I'll top-post as well as answer inline.

http://wiki.apache.org/tomcat/TomcatHibernate
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Charles,
Post by Charles Richard
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
Charles,
Post by Charles Richard
Post by Charles Richard
The tomcat version is 6.0.30.
You should look at the changelog and security reports. I think
you're going to want to upgrade.
Post by Charles Richard
That is something that would be the next priority after
understanding our connection pooling issues. If upgrading Tomcat
would help with this as well, we would definitely try to
fasttrack it. We usually try to change one thing at a time on our
production environment.
A very good policy. No, I don't think this will help you with your
DataSource stuff. But you will want to get this into testing ASAP.
Honestly, you should probably be looking at Tomcat 7 or even Tomcat 8
for getting into your development and/or testing environments.
Post by Charles Richard
Post by Charles Richard
Post by Charles Richard
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.
Post by Charles Richard
I'm really not sure how that could be done either but hopefully
the developers here could help me with this.
Here's a quick wiki document I wrote a while back for that:

http://wiki.apache.org/tomcat/TomcatHibernate
They should be able to do that.
Post by Charles Richard
Post by Charles Richard
Post by Charles Richard
We are not using a context.xml right now, can I use the
context.xml in $TOMCAT_HOME/conf?
No! That's the system-wide defaults for all web applications
deployed to the container. You want META-INF/context.xml within
your WAR file (or exploded WAR-dir).
Post by Charles Richard
Post by Charles Richard
<Host name="www.oursite.com" appBase="/ourpath"
unpackWARs="true" autoDeploy="true"> <Context path=""
docBase="" reloadable="true" />
You should not have a <Context> element at all in your server.xml.
It's a wonder that your application even works with a path of ""
and a docBase of "". I'll bet you have a WEB-INF directory directly
in your webapps/ directory, right?
We've got a WEB-INF folder in our appBase folder reference
(/ourpath/WEB-INF).
Sounds like you've got a mess on your hands.
Post by Charles Richard
I guess.
Move that <Context> into META-INF/context.xml, remove the
"docBase" and "path" attributes, and then add a <Resource> as a
child to configure your DataSource. You'll also need to move your
JDBC driver from WEB-INF/lib into Tomcat's lib/ directory.
Post by Charles Richard
In our /ourpath/META-INF, there is no context.xml, just a
MANIFEST.MF file. I can create one if that's what I'm supposed to
do.
Yep, just create a new one.
- -chris
Hope that helps

Mark
/mde/
Neven Cvetkovic
2014-03-05 00:57:01 UTC
Permalink
On Tue, Mar 4, 2014 at 2:56 PM, Charles Richard <
Post by Charles Richard
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
Post by Charles Richard
Can we still use Hibernate in our Spring application if we
Post by Charles Richard
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
Charles Richard
2014-03-05 17:07:32 UTC
Permalink
On Tue, Mar 4, 2014 at 8:57 PM, Neven Cvetkovic
Post by Neven Cvetkovic
On Tue, Mar 4, 2014 at 2:56 PM, Charles Richard <
Post by Charles Richard
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
Post by Charles Richard
Can we still use Hibernate in our Spring application if we
Post by Charles Richard
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,
Post by Neven Cvetkovic
Charles,
As Chris pointed out - there are two ways you can get a datasource into
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
(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).
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.
Post by Neven Cvetkovic
Once you register your resource with JNDI, you can reference that
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>
(works in newer Spring 3.x, and possibly 2.x)
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
Christopher Schultz
2014-03-05 18:47:43 UTC
Permalink
Charles,
Post by Charles Richard
On Tue, Mar 4, 2014 at 8:57 PM, Neven Cvetkovic
Post by Neven Cvetkovic
On Tue, Mar 4, 2014 at 2:56 PM, Charles Richard <
Post by Charles Richard
On Tue, Mar 4, 2014 at 3:17 PM, Christopher Schultz <
Post by Charles Richard
Can we still use Hibernate in our Spring application if we
Post by Charles Richard
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,
Post by Neven Cvetkovic
Charles,
As Chris pointed out - there are two ways you can get a
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
Post by Charles Richard
Post by Neven Cvetkovic
(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).
Cannot create JDBC driver of class '' for connect URL 'null'
java.lang.NullPointerException
Uh. I hate getting this error. I have honestly never figured out what
the root cause is... the only thing that seems to work is to throw out
the whole <Resource> configuration and start from scratch. I'm fairly
sure it's a typo somewhere. Let's take a look...
Post by Charles Richard
It also seems like Tomcat is not picking up my context.xml. Any suggestions?
<?xml version="1.0" encoding="UTF-8"?> <Context
antiJARLocking="true" path="">
Remove the "path" attribute: it's not legal here. If you want your
webapp to be the "root" context, then you need to name it ROOT.war (or
ROOT directory) within your webapps/ directory. Note that case
matters, even on case-insensitive filesystems like fat, ntfs, and hfs.
Do you really need "autiJarLocking"?
Post by Charles Richard
<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>
You will need a driverClassName in there. For MySQL, it should be
com.mysql.jdbc.Driver if you are using Connector/J (which, of course,
you are). Note that 1800 seconds is a looooong time.

You have also misspelled "minIdle", though the default for minIdle is
already "0".
Post by Charles Richard
<?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" />
This I can't help with.
Post by Charles Richard
<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>
This looks fine to me. My experience is that this section in web.xml
is not strictly necessary.

- -chris

Loading...