Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- <!-- Pooler de connexion -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-dbcp2</artifactId>
- <version>2.5.0</version>
- </dependency>
- */
- @Bean
- public DataSource dataSourceAddresses() {
- String url = env.getProperty("jdbc.urlAddresses");
- String usr = ApplicationVariables.DB_USER != null ? ApplicationVariables.DB_USER : env.getProperty("jdbc.username");
- String pwd = ApplicationVariables.DB_PASSWORD != null ? ApplicationVariables.DB_PASSWORD : env.getProperty("jdbc.password");
- String driver = env.getProperty("jdbc.driverClassName");
- try {
- Class.forName(driver).getConstructors()[0].newInstance();
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
- | SecurityException | ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ConnectionFactory cf = new DriverManagerConnectionFactory(url, usr, pwd);
- PoolableConnectionFactory pcf = new PoolableConnectionFactory (cf, null);
- GenericObjectPool connectionPool = new GenericObjectPool(pcf);
- // connectionPool.setMaxTotal(20); // si on veut limiter à 20 connexion à la BDD en simultané
- pcf.setPool(connectionPool);
- // obligé de surcharger cette classe sinon la méthode 'getConnection(username,pwd) lance une exception (sauf si on est en mode multitenancy)
- PoolingDataSource pds = new PoolingDataSource(connectionPool) {
- public Connection getConnection(final String uname, final String passwd) throws SQLException {
- return super.getConnection();
- }
- };
- return pds;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement