开发者

Is there a way to change an Oracle connection on the fly in Play! Framework?

I currently work in a project where each user has his own schema in Oracle 11g DB. This design was done because all the security roles and privileges to access the Oracle tables are stored in the DB. Ou开发者_如何学编程r team is trying to figure how to do this in the cool Play! framework. Any suggestions?


As far as I understand, you can try wrap a value of DB.datasource with something like this (where currentUser() and currentPassword() should return the credentials of the current user who makes a request):

public class DataSourceWrapper {
    private DataSource original;
    public DataSourceWrapper(DataSource original) {
        this.original = original;
    }

    public Connection getConnection() {
        return original.getConnection(currentUser(), currentPassword());
    }

    ...

    public DataSource getOriginal() {
        return original;
    }
}

Replacement of DB.datasource should happen between execution of onApplicationStart() methods of DB and JPA plugins, so that you need to create a custom plugin:

public class DataSourceReplacementPlugin extends PlayPlugin {
    public void onApplicationStart() {
        DB.datasource = new DataSourceWrapper(DB.datasource);
    }

    public void onApplicationStop() {
        if (DB.datasource instanceof DataSourceWrapper) {
            DB.datasource = ((DataSourceWrapper) DB.datasource).getOriginal();
        }
    }
}

and register in with the appropriate priority level in conf/play.plugins:

350: DataSourceReplacementPlugin
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜