tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephan Beutel <beu...@axivion.com>
Subject Re: Context.xml for multiple datasources
Date Wed, 09 Feb 2011 09:49:37 GMT
  Am 09.02.2011 10:30, schrieb chris derham:
>> I need some help creating my context.xml file in a correct way.
>>
>> My application is based on Spring and Hibernate. I need to configure more
>> than one datasource
>> to access multiple databases at the same time.
>>
>>
> We use JNDI to set up our datasource. So in spring configuration, we have
>
>      <bean id="dataSource"
> class="org.springframework.jndi.JndiObjectFactoryBean">
>          <property name="jndiName" value="java:comp/env/jdbc/myapp" />
>      </bean>
>
> Then in<tomcat_base>\conf\context.xml we have
>
> <Context>
>      <Resource name="jdbc/myapp"
>              auth="Container"
>              type="javax.sql.DataSource"
>              username="<username>"
>              password="<password>"
>              driverClassName="oracle.jdbc.OracleDriver"
>              url="jdbc:oracle:thin:@localhost:1521:xe"
>              maxActive="20"
>              maxIdle="2"
>              minIdle="1"
>              initialSize="2"
>              />
> </Context>
>
> This will provide the jndi data source to all apps in the tomcat instance.
> If you wish/need to restrict that to a single war, place the above entry in
> conf/Catalina/[host]/[appName].xml. Using this technique you should be able
> to specify multiple datasources.
>
> Chris
>
Hello,

thank you for the quick answer.
I know how to configure one datasource and lookup with JNDI. This is my 
actual configuration.

But I need to configure more than one datasource to switch the database 
at runtime.

With my Spring configured datasource it works to access all databases. 
But I don't know how to
create a context for Tomcat to access all databases.

------------------
My configuration to access ONE database:

datasource.xml for Spring:
#########
<bean id="dataSource"
         
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.sqlite.JDBC</value>
</property>
<property name="url">
<value>jdbc:sqlite:C:\firstDatabase.db</value>
</property>
<property name="username">
<value>SA</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
#########

The same datasource as context to lookup with JNDI:
#########
<Context>
     ...
<Resource name="jdbc/myds" auth="Container" type="javax.sql.DataSource"
                maxActive="100" maxIdle="30" maxWait="10000"
                username="sa" password="" driverClassName="org.sqlite.JDBC"
                url="jdbc:sqlite:C:\firstDatabase.db"/>
     ...
</Context>
#########
Lookup in Spring:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myds"/>


------------------------------------
The configuration I need in my application:
datasource.xml for Spring:
########
<bean id="parentDataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
abstract="true">
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="username" value="sa" />
</bean>

<bean id="firstDataSource" parent="parentDataSource">
<property name="url" value="jdbc:sqlite:C:\firstDatabase.db" />
</bean>

<bean id="secondDataSource" parent="parentDataSource">
<property name="url" value="jdbc:sqlite:C:\secondDatabase.db" />
</bean>

<bean id="dataSource" 
class="com.ax.dashboard.datasource.AxRoutingDataSource">
<property name="testString" ref="testContext"/>
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="first" value-ref="firstDataSource" />
<entry key="second" value-ref="secondDataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="firstDataSource" />
</bean>
########

And the question is:
How to build the context to move the existing Spring configuration to 
the Tomcat context to get all datasources with JNDI.
The problem is, that it must be possible to set a variable amount of 
databases. I can't set the number of databases to a fixed amount.

Building the Resources for the beans named parentDataSource, 
firstDataSource and secondDataSource is like configuring a single database.
But How can I create a JNDI lookable resource like the bean named 
dataSource containing a map of all available database resources?

Regards
Stephan


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


Mime
View raw message