tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "LANDRAIN Jean-Pol" <jplandr...@europarl.eu.int>
Subject RE: Tomcat Datasource , can we define them in war file (whithout accessing to Admin console)?
Date Tue, 03 Jan 2006 14:57:13 GMT
Hello,

Note: This works at least since TC 5.5.7; I don't know exactly if it
works for the previous versions.

Just create a context.xml file (where you describe your datasources) in
the META-INF directory of your .war file (unfortunately, you'll still
need the jdbc jar file in common/lib). This file will be loaded when the
application is deployed.

Example of context.xml (for 2 datasources to Oracle DBs) :

<Context path="/Application" docBase="Application" debug="1"
reloadable="true">

	<Resource name="jdbc/datasource1" 
		  auth="Container"
              type="javax.sql.DataSource" 
 
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver" 
              url="jdbc:oracle:thin:@//host1:1528/SID1"
              username="username1" 
              password="password1"
              maxActive="20" 
              maxIdle="10" 
              maxWait="120000"
              removeAbandoned="true" 
		  logAbandoned="true" 
		  removeAbandonedTimeout="300"
		  testOnBorrow="true"
		  testOnReturn="true"
		  validationQuery="select 1 from dual" />

			  
	<Resource name="jdbc/datasource2" 
		  auth="Container"
              type="javax.sql.DataSource" 
 
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver" 
              url="jdbc:oracle:thin:@//host2:1521/SID2"
              username="username2" 
              password="password2"
              maxActive="20" 
              maxIdle="10" 
              maxWait="120000" 
              removeAbandoned="true" 
		  logAbandoned="true" 
		  removeAbandonedTimeout="300"
		  testOnBorrow="true"
		  testOnReturn="true"
		  validationQuery="select 1 from dual" />


</Context>

The datasources must then be referenced from the WEB-INF/web.xml file
(to allow the lookup in the JNDI context of TC) :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    	 version="2.4">
	[...]
	<resource-ref>
  		<description>
  			Resource ref to the Datasource configured in
context.xml
  			ojdbc14.jar must be copied to
$CATALINA_HOME/common/lib
  		</description>
  		<res-ref-name>jdbc/datasource1</res-ref-name>
  		<res-type>javax.sql.DataSource</res-type>
  		<res-auth>Container</res-auth>
	</resource-ref>	
	<resource-ref>
  		<description>
  			Resource ref to the Datasource configured in
context.xml
  			ojdbc14.jar must be copied to
$CATALINA_HOME/common/lib
  		</description>
  		<res-ref-name>jdbc/datasource2</res-ref-name>
  		<res-type>javax.sql.DataSource</res-type>
  		<res-auth>Container</res-auth>
	</resource-ref>
</web-app>

The lookup is done like this (without the try/catch blocks) :

Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env"); // I usually
cache envCtx to avoid recreating it
DataSource ds = (DataSource)envCtx.lookup("jdbc/datasource1");


There is probably too much in this answer, but it will probably help for
similar questions too.

Best Regards,
Jean-Pol.

-----Original Message-----
From: Khawaja Shams [mailto:ksshams@gmail.com] 
Sent: 27 December 2005 02:31
To: Tomcat Users List
Subject: Re: Tomcat Datasource , can we define them in war file
(whithout accessing to Admin console)?

Hello,
   I am assuming that you are trying to define this datasource in order
to
do application server managed connection pooling.  The closest I have
done
to what you described is declare a datasource as a global resource in
the
server.xml and refer to it in context definition.  You can define a new
context in the $CATALINA_HOME/conf/Catalina/localhost/APPNAME.xml. Here
is
an example:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/son">
  <ResourceLink
            name="jdbc/teamDB"
            global="jdbc/teamDB"
            type="javax.sql.DataSource"
          />
</Context>

 However, I still needed to put the jdbc jar file in the common/lib.  I
am
not completely sure about this, but for application server to manage
your
datasource, one needs to make the proper jar file available to the app
server by putting it in the common/lib as it will not look inside each
deployed directory to perform application independent tasks.  I am also
curious to know if someone has found a way around this.


Best Regards,
Khawaja Shams

On 12/26/05, Legolas Woodland <legolas.w@gmail.com> wrote:
>
> Hi
> Thank you for reading my post.
> Is it possible to make a data-source without admin console ?
> I mean by defining the data-source in web.xml or in Context.xml (i
think
> i read somewhere that we could put context.xml into meta-inf folder
and
> it will act like Context definition in admin console).
>
> is it mandatory that Tomcat shared library folder contain my database
> jdbc driver to have data-source ?
> I mean can we bundle , our JDBC driver inside war file and define the
> data-source in war file too ?
>
> ---------------------------------------------------------------------
> 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


Mime
View raw message