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 14:21:59 GMT
  Am 09.02.2011 15:02, schrieb Caldarale, Charles R:
>> From: Stephan Beutel [mailto:beutel@axivion.com]
>> Subject: Re: Context.xml for multiple datasources
>> But I thought it could be defined by a map in
>> context.xml like in Spring.
> Even if you did, that wouldn't solve your problem.  You would still have to stop and
restart the webapp every time an additional datasource was added to the<Context>  element.
>
> You could provide a<Parameter>  element nested inside the<Context>  that
specifies the names of the<Resource>  entries, but there's no automatically created
list of<Resource>  elements.
>
>   - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and
is thus for use only by the intended recipient. If you received this in error, please contact
the sender and delete the e-mail and its attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
Restarting the webapp is no problem. Rebuilding the war file is a problem.

At the moment I solved it in this was:

<Resource name="jdbc/first" 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"/>

<Resource name="jdbc/second" auth="Container" type="javax.sql.DataSource"
                maxActive="100" maxIdle="30" maxWait="10000"
                username="sa" password="" driverClassName="org.sqlite.JDBC"
                url="jdbc:sqlite:C:\secondDatabase.db"/>

<Resource name="jdbc/third" auth="Container" type="javax.sql.DataSource"
                maxActive="100" maxIdle="30" maxWait="10000"
                username="sa" password="" driverClassName="org.sqlite.JDBC"
                url="jdbc:sqlite:C:\thirdDatabase.db"/>

<Environment name="dataSourceNames" value="first, second, third" 
type="java.lang.String"
                 override="false"/>

<Environment name="dataSourceList" value="jdbc/first, jdbc/second, 
jdbc/third" type="java.lang.String"
                 override="false"/>


Spring looks for the two environment vars (dataSourceNames, 
dataSourceList) and inject them into my RoutingDataSource class:

<jee:jndi-lookup id="dataSources" jndi-name="dataSourceList" />
<jee:jndi-lookup id="dataSourceNames" jndi-name="dataSourceNames" />

<bean id="dataSource" 
class="com.ax.dashboard.datasource.AxRoutingDataSource">

<property name="dataSources" ref="dataSources"/>
<property name="dataSourceNames" ref="dataSourceNames" />
</bean>

And in my java class I do the lookup with JNDI:

private void createDataSourceMap() {
         if (this.dataSourceList != null && this.dataSourceNames != null 
&& this.dataSourceList.size() > 0
&& this.dataSourceNames.size() > 0 && dataSourceList.size() == 
this.dataSourceNames.size()) {
             this.dataSources = new HashMap<Object, Object>();
             for (int i = 0; i < dataSourceList.size(); i++) {
                 Context initCtx;
                 Object dataSource = null;
                 try {
                     initCtx = new InitialContext();
                     Context envCtx = (Context) 
initCtx.lookup("java:comp/env");
                     dataSource = envCtx.lookup(dataSourceList.get(i));
                     if(i==0){
                         setDefaultTargetDataSource(dataSource);
                     }

                 } catch (NamingException e) {
                     e.printStackTrace();
                 }
                 dataSources.put(dataSourceNames.get(i), dataSource);
             }
             setTargetDataSources(dataSources);
         }
     }

If someone has a better solution, please let me know.

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