You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Arttu Tanner <ar...@invian.fi> on 2011/09/30 05:17:19 UTC

Defining JDBC resource and AutoDeploy

OS: Linux / CentOS / 2.6.18-028stab092.1
Tomcat: 5.5.23

I have simple XML-RPC -WebApp, that uses MySQL database.
Currently I have defined the JDBC resource in server.xml inside the
<Host> -tags as follows:

<Context docBase="mywebapp" path="/mywebapp" reloadable="true"
source="org.eclipse.jst.j2ee.server:mywebapp">
    <Resource name="jdbc/MyDB"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
        url="jdbc:mysql://server.com:3306/db_name"
        username="db_username"
        password="db_password"
        maxActive="20"
        maxIdle="10"
        maxWait="5"
        validationQuery="SELECT 1"
        testOnBorrow="TRUE"
        testWhileIdle="TRUE"
        timeBetweenEvictionRunsMillis="10000"
        minEvictableIdleTimeMillis="60000"
    />
</Context>

In the WebApp the connection is formed like this:

DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");
Connection conn=ds.getConnection();

This works fine, but when resource is defined in server.xml, WebApp
AutoDeploy won't work and even the manager application can't undeploy
it. If I take the  <Context> -element out of the server.xml (and put
it elsewhere) AutoDeploy works, but database connections throw:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

I've tried to put the context element in several different files, including:
-webapps/mywebapp/WEB-INF/context.xml
-context.xml in SERVERROOT/conf/
-server.xml, only the <Resource> -tag in <GlobalNamingResources> -element

None of these work, and I end up with the same exception.

There must be a way to get both the MySQL connection and
AutoDeployment at the same time.
Can somebody point me to right direction?

-Arttu

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining JDBC resource and AutoDeploy

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Arttu,

On 10/1/2011 6:04 PM, Arttu Tanner wrote:
> Ok, figured it out. I needed to use InitialContext.lookupLink()
> instead of InitialContext.lookup() what I was using before.

That should not be necessary. Here is the code I have been using for
years using MySQL Connector/J and Tomcat 5.5, 6.0, and 7.0:

        // dataSourceName looks like java:/comp/env/jdbc/myDataSource
        Context context = new InitialContext();
        DataSource ds = (DataSource)context.lookup(dataSourceName);
        if(null == ds)
            throw new NamingException("Found no DataSource for '"
                                      + dataSourceName + "'");
        return ds.getConnection();

It's best to define everything in META-INF/context.xml. Then you don't
need anything anywhere else.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk6J6j4ACgkQ9CaO5/Lv0PBC+ACdHIk6h7Jj2oHN+i+rbsAOhJvd
goMAni7mcKwcdp66b90/SNGXqndw8N/y
=bNIC
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining JDBC resource and AutoDeploy

Posted by Arttu Tanner <ar...@invian.fi>.
Ok, figured it out.
I needed to use InitialContext.lookupLink() instead of
InitialContext.lookup() what I was using before.
Seems obvious now, but I didn't think there would be two similar methods.
I just thought I'll post my solution for further reference. It's
always annoying to find that somebody has the same problem as you, but
then nobody gives the solution...

-Artttu

2011/9/30 Tim Watts <ti...@cliftonfarm.org>:
> Try:
>      * conf/Catalina/localhost/mywebapp.xml  OR
>      * webapps/mywebapp/META-INF/context.xml  OR
>      * If you put it in GlobalNamingResources then you have to add a
>        <Resource-Link> to <Context>. See the Configuration
>        documentation.
>
> http://tomcat.apache.org/tomcat-5.5-doc/config/context.html has all the
> details.
>
>
> On Fri, 2011-09-30 at 06:17 +0300, Arttu Tanner wrote:
>> OS: Linux / CentOS / 2.6.18-028stab092.1
>> Tomcat: 5.5.23
>>
>> I have simple XML-RPC -WebApp, that uses MySQL database.
>> Currently I have defined the JDBC resource in server.xml inside the
>> <Host> -tags as follows:
>>
>> <Context docBase="mywebapp" path="/mywebapp" reloadable="true"
>> source="org.eclipse.jst.j2ee.server:mywebapp">
>>     <Resource name="jdbc/MyDB"
>>         auth="Container"
>>         type="javax.sql.DataSource"
>>         driverClassName="com.mysql.jdbc.Driver"
>>         factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
>>         url="jdbc:mysql://server.com:3306/db_name"
>>         username="db_username"
>>         password="db_password"
>>         maxActive="20"
>>         maxIdle="10"
>>         maxWait="5"
>>         validationQuery="SELECT 1"
>>         testOnBorrow="TRUE"
>>         testWhileIdle="TRUE"
>>         timeBetweenEvictionRunsMillis="10000"
>>         minEvictableIdleTimeMillis="60000"
>>     />
>> </Context>
>>
>> In the WebApp the connection is formed like this:
>>
>> DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");
>> Connection conn=ds.getConnection();
>>
>> This works fine, but when resource is defined in server.xml, WebApp
>> AutoDeploy won't work and even the manager application can't undeploy
>> it. If I take the  <Context> -element out of the server.xml (and put
>> it elsewhere) AutoDeploy works, but database connections throw:
>> javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
>>
>> I've tried to put the context element in several different files, including:
>> -webapps/mywebapp/WEB-INF/context.xml
>> -context.xml in SERVERROOT/conf/
>> -server.xml, only the <Resource> -tag in <GlobalNamingResources> -element
>>
>> None of these work, and I end up with the same exception.
>>
>> There must be a way to get both the MySQL connection and
>> AutoDeployment at the same time.
>> Can somebody point me to right direction?
>>
>> -Arttu
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining JDBC resource and AutoDeploy

Posted by Tim Watts <ti...@cliftonfarm.org>.
Try:
      * conf/Catalina/localhost/mywebapp.xml  OR
      * webapps/mywebapp/META-INF/context.xml  OR
      * If you put it in GlobalNamingResources then you have to add a
        <Resource-Link> to <Context>. See the Configuration
        documentation.

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html has all the
details.


On Fri, 2011-09-30 at 06:17 +0300, Arttu Tanner wrote:
> OS: Linux / CentOS / 2.6.18-028stab092.1
> Tomcat: 5.5.23
> 
> I have simple XML-RPC -WebApp, that uses MySQL database.
> Currently I have defined the JDBC resource in server.xml inside the
> <Host> -tags as follows:
> 
> <Context docBase="mywebapp" path="/mywebapp" reloadable="true"
> source="org.eclipse.jst.j2ee.server:mywebapp">
>     <Resource name="jdbc/MyDB"
>         auth="Container"
>         type="javax.sql.DataSource"
>         driverClassName="com.mysql.jdbc.Driver"
>         factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
>         url="jdbc:mysql://server.com:3306/db_name"
>         username="db_username"
>         password="db_password"
>         maxActive="20"
>         maxIdle="10"
>         maxWait="5"
>         validationQuery="SELECT 1"
>         testOnBorrow="TRUE"
>         testWhileIdle="TRUE"
>         timeBetweenEvictionRunsMillis="10000"
>         minEvictableIdleTimeMillis="60000"
>     />
> </Context>
> 
> In the WebApp the connection is formed like this:
> 
> DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");
> Connection conn=ds.getConnection();
> 
> This works fine, but when resource is defined in server.xml, WebApp
> AutoDeploy won't work and even the manager application can't undeploy
> it. If I take the  <Context> -element out of the server.xml (and put
> it elsewhere) AutoDeploy works, but database connections throw:
> javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
> 
> I've tried to put the context element in several different files, including:
> -webapps/mywebapp/WEB-INF/context.xml
> -context.xml in SERVERROOT/conf/
> -server.xml, only the <Resource> -tag in <GlobalNamingResources> -element
> 
> None of these work, and I end up with the same exception.
> 
> There must be a way to get both the MySQL connection and
> AutoDeployment at the same time.
> Can somebody point me to right direction?
> 
> -Arttu
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org