8bitboy
8bitboy

Reputation: 63

Java Datasource, how to dispose it

I'm working on a webapp where i manually create my DataSource. (also see my other question why: How to use Spring to manage connection to multiple databases) because I need to connect to other databases (dev, prod, qa, test).

Now I have solved it to choose and switch between databases. But if a user logs out of my app. He wants to try to connect to an other database. He is still connected to the same datasource because at runtime the myDs is not null. How can I properly dispose of this Datasource when user logs out? I don't want the user to create the datasource every time he queries the database.

private DataSource createDataSource(Environment e) {
    OracleDataSource ds = null;        
    String url = null;
    try {
        if (myDs != null) {
            logger.info("myDs connection: " + etmetaDs.getConnection().getMetaData().getURL());
            url = myDs.getConnection().getMetaData().getURL();
        }
    } catch (SQLException exc) {
        // TODO Auto-generated catch block
        exc.printStackTrace();
    }

    if (myDs == null) {            
        try {
            ds = new OracleDataSource();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        ds.setDriverType("oracle.jdbc.OracleDriver");
        ds.setURL(e.getUrl());
        try {
            Cryptographer c = new Cryptographer();
            ds.setUser(c.decrypt(e.getUsername()));
            ds.setPassword(c.decrypt(e.getPassword()));
        } catch (CryptographyException ex) {
            logger.error("Failed to connect to my environment [" + e.getName() + "]");
            ex.printStackTrace();
            return null;
        }
        logger.info("Connecting to my environment [" + e.getName() + "]");

        myDs = ds;
    } else if (url.equals(e.getUrl())) {

    } else {

    }

    return myDs;
}

Upvotes: 0

Views: 615

Answers (1)

mki
mki

Reputation: 635

If you read the answer of Reza in you other question you can see how to create multiple DataSource.
I think here that the problem is not the DataSource but the way you store information in your code. I suppose that your etmetaDs is shared but all your users, so dispose it when a user log out (= set it to null) is not the good option.

What you have to do, is to maintain the status of the connection for each user. And when a user log off, you can reset is status in order to obtain a new connection the next time it connects.

Update: There are many way to achieve this. I give here an example of what I imagine, but you have to adapt it to your needs. Suppose that you have a UserData object that holds information :

public class UserData
{
String id;
String name;
String database;
}

You may have in your application a dropdown with the name of the database (dev, test, ...) with an empty first item. When the user selects a database, you get the connection with createDataSource(). If it already exists you returns the DataSource else you create a new one. When your user disconnect (or when the user log on), you set the database to "" to force him to select the database in the dropdown. There is no need to reset the datasource.

Upvotes: 1

Related Questions