Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1.    
  2. /**
  3.   <!--  Pooler de connexion -->
  4.         <dependency>
  5.             <groupId>org.apache.commons</groupId>
  6.             <artifactId>commons-dbcp2</artifactId>
  7.             <version>2.5.0</version>
  8.         </dependency>
  9. */
  10. @Bean
  11.     public DataSource dataSourceAddresses() {      
  12.         String url = env.getProperty("jdbc.urlAddresses");
  13.         String usr =  ApplicationVariables.DB_USER != null ? ApplicationVariables.DB_USER : env.getProperty("jdbc.username");
  14.         String pwd = ApplicationVariables.DB_PASSWORD != null ? ApplicationVariables.DB_PASSWORD : env.getProperty("jdbc.password");
  15.         String driver = env.getProperty("jdbc.driverClassName");
  16.        
  17.         try {
  18.             Class.forName(driver).getConstructors()[0].newInstance();
  19.         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
  20.                 | SecurityException | ClassNotFoundException e) {
  21.             // TODO Auto-generated catch block
  22.             e.printStackTrace();
  23.         }
  24.         ConnectionFactory cf = new DriverManagerConnectionFactory(url, usr, pwd);
  25.         PoolableConnectionFactory pcf = new PoolableConnectionFactory (cf, null);
  26.         GenericObjectPool connectionPool = new GenericObjectPool(pcf);
  27.         // connectionPool.setMaxTotal(20); // si on veut limiter à 20 connexion à la BDD en simultané
  28.         pcf.setPool(connectionPool);
  29.         // obligé de surcharger cette classe sinon la méthode 'getConnection(username,pwd) lance une exception (sauf si on est en mode multitenancy)
  30.         PoolingDataSource pds = new PoolingDataSource(connectionPool) {
  31.              public Connection getConnection(final String uname, final String passwd) throws SQLException {
  32.                  return super.getConnection();
  33.              }
  34.         };
  35.         return pds;
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement