What to change for JDBC connection in Spring Security


 What to change for JDBC connection in Spring Security?

  • Add annotation @PropertySource("classpath:persistance-mysql.properties") in AppConfig  

  • setup variables to hold properties

@Autowired private Environment env;  

  • setup a logger for diagnostics

private Logger logger = Logger.getLogger(getClass().getName());  

  • define a bean for security datasource 

@Bean 

public DataSource securityDataSource() { 

 

//create connection pool 

ComboPooledDataSource securityDataSource = new ComboPooledDataSource(); 

 

//set jdbc driver class 

try { 

securityDataSource.setDriverClass(env.getProperty("jdbc.driver")); 

}catch(PropertyVetoException exc) { 

throw new RuntimeException(exc); 

 

//log connection properties 

//to make sure data we are reading is correct 

logger.info(">>> jdbc.url=" +env.getProperty("jdbc.url")); 

logger.info(">>> jdbc.user=" +env.getProperty("jdbc.user")); 

 

//set database connection properties 

securityDataSource.setJdbcUrl(env.getProperty("jdbc.url")); 

securityDataSource.setUser(env.getProperty("jdbc.user")); 

securityDataSource.setPassword(env.getProperty("jdbc.password")); 

 

//set connection pool properties 

securityDataSource.setInitialPoolSize( 

getIntProperty("connection.pool.initialPoolSize")); 

 

securityDataSource.setMinPoolSize( 

getIntProperty("connection.pool.minPoolSize")); 

 

securityDataSource.setMaxPoolSize( 

getIntProperty("connection.pool.maxPoolSize")); 

 

securityDataSource.setMaxIdleTime( 

getIntProperty("connection.pool.maxIdleTime")); 

 

return securityDataSource; 

 




  • Create a helper method to read environment property and convert to int 

 

private int getIntProperty(String propName) { 

 

String propVal = env.getProperty(propName); 

 

//convert to int 

int intPropVal = Integer.parseInt(propVal); 

return intPropVal; 


Comments

Popular posts from this blog

Depression-A life taking mind disorder

Benefits of Giloy or Tinospora