How to set a role in DataSource from Spring

别说谁变了你拦得住时间么 提交于 2019-12-04 18:11:18

DBCP's BasicDataSource allows you to pass SQL statements to be executed during initialization of the connection. Something like this:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close">
    <property name = "connectionInitSqls">
        <list><value>set role ROLE_ACCESS_SELECT</value></list>
    </property>
    ...
</bean>

Never mind, I have fixed this issue. Thanks to all who looked at my post the solution is like this:

add this line after minIdle property:

<property name="initSQLCommands">
            <list>
                <value>SET ROLE ROLE_WEB_OPERATORS</value>
            </list>
        </property>

and define a class such as:

public class ExtendedBasicDataSource extends BasicDataSource {
    public void setInitSQLCommands(List<String> initSQLCommands) {
        this.initSQLCommands = initSQLCommands;
        setConnectionInitSqls(initSQLCommands);
    }

}

And use that bean instead of org.apache.commons.dbcp.BasicDataSource while defining the datasource bean in Spring's ApplicationContext.xml.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!