简体   繁体   English

Java数据源,如何处理

[英]Java Datasource, how to dispose it

I'm working on a webapp where i manually create my DataSource. 我正在使用Webapp手动创建数据源。 (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). (另请参见我的另一个问题原因: 如何使用Spring管理与多个数据库的连接 ),因为我需要连接至其他数据库(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. 他仍然连接到相同的数据源,因为在运行时myDs不为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;
}

If you read the answer of Reza in you other question you can see how to create multiple DataSource. 如果您在其他问题中读到Reza的答案,则可以看到如何创建多个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. 我想您的etmetaD是共享的,但您的所有用户都是共享的,所以当用户注销(=将其设置为null)时,处置它不是一个好选择。

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 : 假设您有一个包含信息的UserData对象:

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. 您的应用程序中可能有一个带有数据库名称的下拉列表(dev,test等),其中第一项为空。 When the user selects a database, you get the connection with createDataSource(). 当用户选择数据库时,将获得与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. 无需重置数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM