Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Our team is trying to figure how to do this in the cool Play! framework. Any suggestions?

like image 248
AsemRadhwi Avatar asked Jan 26 '26 18:01

AsemRadhwi


1 Answers

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
like image 135
axtavt Avatar answered Jan 29 '26 07:01

axtavt