change same server db based on client login in spring 4 mvc

103 Views Asked by At

I have mysql server in linux server. It contains 5 database for 5 different clients. so far, I have created 5 tomcat instances to deploy 5 war based on client and accessing database from servelet.xml file like below,

client1 instance war :

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/client1" />
    <property name="username" value="rootuser" />
    <property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource"/> 
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
    <constructor-arg ref="dataSource"/>
</bean>

client2 instance war :

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/client2" />
    <property name="username" value="rootuser" />
    <property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource"/> 
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
    <constructor-arg ref="dataSource"/>
</bean>

Config class :

public class JdbcRepository {

    @Autowired
    @Qualifier("jdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("namedParameterJdbcTemplate")
    protected NamedParameterJdbcTemplate  namedParameterJdbcTemplate ;

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource; //mysqlSource;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        jdbcTemplate = new JdbcTemplate(this.dataSource);
    }

    public Long findMemberIdByUserName(String username) {
        try{
        String sql = "SELECT userLoginId FROM user WHERE userName = ?";
        Long id = jdbcTemplate.queryForObject(sql, new Object[]{username}, Long.class);
        return id;
        }
        catch(Exception e){
            return 0l;
        }
    }

But now I want to create single instance and deploy single war for all clients and access the database based on user login. Like below,

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/{DBBasedOnLoginUser}" />
    <property name="username" value="rootuser" />
    <property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource"/> 
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
    <constructor-arg ref="dataSource"/>
</bean>

How to achieve this?

1

There are 1 best solutions below

0
On

So, you want to communicate with different databases depending upon some condition.

Changing datasource connection url runtime

This is exactly what you need.