0
votes

I already connect databse using datasource using

<Resource name="jdbc/common" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="user" password="pass" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db"/>


but we have over 50 database. So I want to connect database dynamically . Please help me.

2

2 Answers

0
votes

You can use apache commons dbcp to programatically create a data source:

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(DATABASE_DRIVER_CLASS);
ds.setUsername(DATABASE_USERNAME);
ds.setPassword(DATABASE_PASSWORD);
ds.setUrl(DATABASE_URL);
ds.setInitialSize(1);
ds.setMaxActive(50);
ds.setDefaultAutoCommit(false);

http://commons.apache.org/proper/commons-dbcp/

0
votes

50 databases? Do you mean "50 databases used by a single application"?

If yes - that's nuts.

Database configuration in Java EE apps is usually added to the application server (Tomcat or JBOSS). The application does a JNDI lookup to get a reference to the data source out of a pool.

If you have fifty JNDI data sources for a single app, I'd say you should rethink your design.