title: Dynamically (Runtime) change Datasource credentials in Spring Mybatis
body: <p><strong>I want to dynamically change Datasource properties in Spring+MyBatis project.</strong></p>
  
  <p>Problem is in Spring + MyBatis integration, we cannot set the datasource properties dynamically during runtime.</p>
  
  <p><em>Currently I'm using the following code to set the credentials:</em></p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}" p:username="${jdbc.username}"
      p:password="${jdbc.password}" /&gt;
  </code></pre>
  
  <p>I tried options with UserCredentialsDataSourceAdapter to change the password during runtime but I cannot return back the dataSource object to use for the connection as MyBatis</p>
  
  <pre><code>ApplicationContext context = ApplicationContextUtils.getApplicationContext();
  UserCredentialsDataSourceAdapter ds = (UserCredentialsDataSourceAdapter) context.getBean("dataSource");
  ds.setCredentialsForCurrentThread("test", "test");
  </code></pre>
  
  <p>I'm stuck here, I cannot use the dataSource element <code>ds</code> to use for making connection for MyBatis. Please help me in resolving this issue.</p>