Re: Context.xml for multiple datasources

2011-02-09 Thread Stephan Beutel

 Am 09.02.2011 15:02, schrieb Caldarale, Charles R:

From: Stephan Beutel [mailto:beu...@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  element.

You could provide a  element nested inside the  that specifies the 
names of the  entries, but there's no automatically created list of  
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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@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:







type="java.lang.String"

override="false"/>


override="false"/>


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





class="com.ax.dashboard.datasource.AxRoutingDataSource">






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();
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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Context.xml for multiple datasources

2011-02-09 Thread Caldarale, Charles R
> From: Stephan Beutel [mailto:beu...@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  element.

You could provide a  element nested inside the  that 
specifies the names of the  entries, but there's no automatically 
created list of  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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Context.xml for multiple datasources

2011-02-09 Thread Stephan Beutel

 Am 09.02.2011 12:12, schrieb chris derham:

  That's right, my code doesn't support a variable amount of databases at
the moment because
the databases are configured in Spring (inside my war file).
But if I use the context to configure this list of datasources, the admin
can create databases as he need.
And that's the reason why my configuration has to take place in the server
context and not hardcoded in the Spring configuration.
In Spring I can add more databases as I want, but I have to rebuild my war
file after change the datasource.
With a context configuration my war file can handle as many databases as
the admin configures.
The switch is done by the key of the map.


So why don't you have a string resource in the context that provides the
jndi names of all the jndi data sources, e.g.
mydatasourceJndiNames=name1,name2,name3. Then have some code the lazily
loads this string, splits it, and then loads these via jndi into your map?

Chris


That's the thing I work on at the moment.
But I thought it could be defined by a map in context.xml like in Spring.

Stephan

--
*Stephan Beutel (B.Eng.)*
Senior Software Developer

Axivion GmbH
Nobelstr. 15
70569 Stuttgart
Germany

Tel: +49 711 6204378-66
Fax: +49 711 6204378-99

Mail: beu...@axivion.com 
URL: http://www.axivion.com

Geschaeftsfuehrung: Stefan Bellon, Thomas Eisenbarth, Sebastian Rummler
Sitz der Gesellschaft: Stuttgart
Registergericht: Amtsgericht Stuttgart, HRB 720590



Re: Context.xml for multiple datasources

2011-02-09 Thread chris derham
>
>  That's right, my code doesn't support a variable amount of databases at
> the moment because
> the databases are configured in Spring (inside my war file).
> But if I use the context to configure this list of datasources, the admin
> can create databases as he need.
> And that's the reason why my configuration has to take place in the server
> context and not hardcoded in the Spring configuration.
> In Spring I can add more databases as I want, but I have to rebuild my war
> file after change the datasource.
> With a context configuration my war file can handle as many databases as
> the admin configures.
> The switch is done by the key of the map.
>

So why don't you have a string resource in the context that provides the
jndi names of all the jndi data sources, e.g.
mydatasourceJndiNames=name1,name2,name3. Then have some code the lazily
loads this string, splits it, and then loads these via jndi into your map?

Chris


Re: Context.xml for multiple datasources

2011-02-09 Thread Stephan Beutel

 Am 09.02.2011 11:03, schrieb chris derham:

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?


> From your original post, in which you stated "This is my working Spring
datasource.xml:"











In spring you are building a map of data sources that are also defined in
spring. Why not build a map of data sources that are defined in context.xml
and spring looks them up? You say that you need variable amounts of
databases, but the code you posted and said "this worked" doesn't support
this - to add a new datasource you would have to update spring config. If
you really need variable databases, then some custom code to load a custom
configuration of such data sources might be the way to go. Then just use a
resource element in context to get tomcat to load that for the instance.

Chris

That's right, my code doesn't support a variable amount of databases at 
the moment because

the databases are configured in Spring (inside my war file).
But if I use the context to configure this list of datasources, the 
admin can create databases as he need.
And that's the reason why my configuration has to take place in the 
server context and not hardcoded in the Spring configuration.
In Spring I can add more databases as I want, but I have to rebuild my 
war file after change the datasource.
With a context configuration my war file can handle as many databases as 
the admin configures.

The switch is done by the key of the map.

Stephan

--
*Stephan Beutel (B.Eng.)*
Senior Software Developer

Axivion GmbH
Nobelstr. 15
70569 Stuttgart
Germany

Tel: +49 711 6204378-66
Fax: +49 711 6204378-99

Mail: beu...@axivion.com 
URL: http://www.axivion.com

Geschaeftsfuehrung: Stefan Bellon, Thomas Eisenbarth, Sebastian Rummler
Sitz der Gesellschaft: Stuttgart
Registergericht: Amtsgericht Stuttgart, HRB 720590



Re: Context.xml for multiple datasources

2011-02-09 Thread chris derham
> 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?
>

>From your original post, in which you stated "This is my working Spring
datasource.xml:"











In spring you are building a map of data sources that are also defined in
spring. Why not build a map of data sources that are defined in context.xml
and spring looks them up? You say that you need variable amounts of
databases, but the code you posted and said "this worked" doesn't support
this - to add a new datasource you would have to update spring config. If
you really need variable databases, then some custom code to load a custom
configuration of such data sources might be the way to go. Then just use a
resource element in context to get tomcat to load that for the instance.

Chris


Re: Context.xml for multiple datasources

2011-02-09 Thread Stephan Beutel

 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

 
 
 

Then in\conf\context.xml we have


 


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:
#

class="org.springframework.jdbc.datasource.DriverManagerDataSource">


org.sqlite.JDBC


jdbc:sqlite:C:\firstDatabase.db


SA





#

The same datasource as context to lookup with JNDI:
#

...

...

#
Lookup in Spring:




The configuration I need in my application:
datasource.xml for Spring:

class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
abstract="true">













class="com.ax.dashboard.datasource.AxRoutingDataSource">












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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Context.xml for multiple datasources

2011-02-09 Thread 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





Then in \conf\context.xml we have





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