Francisco Alvarado
Francisco Alvarado

Reputation: 2813

Dynamically configuring datasources

The situation is this: a table that is used in a query (name it SUMMARY) was formerly in the same server and database that I'm doing all the queries of the application (name it server1 and DB1). But recently the SUMMARY table was deleted from this database, which makes it necessary to consult other server / database combinations.

The data from the server name and the database to be used for access to the SUMMARY table are parameterized in a table for this purpose. These data depend on the database which is connected, this way: for example, if I'm in the database DB1 of server1 then parameters will be server21 and DB21, whereas if someone refers to the parameters from the DB5 from server1 parameters will be server16 and DB16.

On that side I have no problem because I have listed the SQL query of the two parameters, ready to give the name of the server and database to consult in each case. This query is necessary in order to give server name and database name with which dynamically generate the datasource to connect to.

The problem, and the topic of this entry is, whether anyone has ever had to dynamically configure the datasource to be used in hibernate.properties, since this is usually a single, fixed value and in this case should be allowed changes in order to view the SUMMARY table (using parameters retrieved by my SQL query) only in this specific case, while all other database operations must be performed by using the original connection properties.

That is: What I need is to dynamically generate the datasource based on the parameters coming from the query, so the approaches which handle this by knowing beforehand how many and what are the possible connections should be discarded because they are not viable to solve my problem.

The application specifications are:

Thanks in advance to anyone who has that knowledge and willing to share.

Upvotes: 2

Views: 2389

Answers (3)

user7871477
user7871477

Reputation: 1

or extend org.apache.commons.dbcp.BasicDataSource

public class MyDataSource extends BasicDataSource {
private void init() {
    username = ...
    password = ...
    url = ...
}

@Override
protected synchronized DataSource createDataSource() throws SQLException {
    init();
    return super.createDataSource();
}
}

Upvotes: 0

Soronthar
Soronthar

Reputation: 1601

You can use a ConnectionProvider in hibernate to decide how to get the connection to be used by a session. We use something like this in our application:

    public Connection getConnection() throws SQLException {
        DataSource ds = (DataSource) BeanFactory.getBean("dataSource" + UserInfo.getDSName());
        return ds.getConnection();
}

UserInfo is a class that store stuff in a ThreadLocal. This code will chose a datasource from Spring depending on the name that was passed in the ThreadLocal. What we do is to set the name of the datasource we want to use before opening the session (the actual logic is a little bit more complicated than that, as it depends on user preferences and other stuff).

You can do something like that to choose what Database to connect to.

You can check the javadocs for the ConnectionProvider interface. Make your own implementation, set the hibernate.connection.provider_class property in your hibernate configuration file to point to your class, and you're done.

Upvotes: 1

jny
jny

Reputation: 8057

I guess, it depends on the number of database/server combinations and the frequency of using them, but if there are a lot of database/servers and low use frequency, using plain JDBC without Hibernate and data sources might be an option. I don't think Hibernate is meant for situations like that.

Upvotes: 0

Related Questions