Ola pessoal!
1) Qual destas classes está correto o uso de um ou mais conexões de bancos de dados diferentes? (mysql, oracle etc)
2) Dentro da classe, qual destes metodos está correto?
Está faltando alguma importação e/ou configuração no Build Path? (ComboPooledDataSource está sem importação)
Para que serve cada metodo abaixo? .setMaxPoolSize(20);.setMinPoolSize(5); .setInitialPoolSize(5); .setMaxIdleTime(1800); .setMaxConnectionAge(1800);
Primeira classe de pool de conexões:
package br.com.caelum.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource; //opcao 01
import org.apache.commons.dbcp.*; //opcao 02
public class ConnectionPool {
private final DataSource dataSource;
//opcao 01
public ConnectionPool() {
MysqlConnectionPoolDataSource pool = new MysqlConnectionPoolDataSource();
pool.setDriverClass("com.mysql.jdbc.Driver");
pool.setUrl("jdbc:mysql://localhost:3306/mydatabase");
pool.setUser("root");
pool.setPassword("");
this.dataSource = pool;
}
//opcao 02
public ConnectionPool() {
ComboPooledDataSource pool = new ComboPooledDataSource(); //está sem importação
pool.setDriverClass("com.mysql.jdbc.Driver");
pool.setUser("root");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql:<caminhoDoSeuBanco>");
pool.setUnreturnedConnectionTimeout(60);
pool.setDebugUnreturnedConnectionStackTraces(true);
pool.setMaxPoolSize(20); //números de conexões?
pool.setMinPoolSize(5);
pool.setInitialPoolSize(5);
pool.setMaxIdleTime(1800);
pool.setMaxConnectionAge(1800);
this.dataSource = pool;
}
public Connection getConnection() throws SQLException {
System.out.print("adiquirindo conexão...");
Connection connection = dataSource.getConnection();
System.out.println(" ok");
return connection;
}
}
Segunda classe de pool de conexões:
public class BasicConnectionPool implements ConnectionPool {
private String url;
private String user;
private String password;
private List<Connection> connectionPool;
private List<Connection> usedConnections = new ArrayList<>();
private static int INITIAL_POOL_SIZE = 10;
public static BasicConnectionPool create(
String url, String user,
String password) throws SQLException {
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
pool.add(createConnection(url, user, password));
}
return new BasicConnectionPool(url, user, password, pool);
}
// standard constructors
@Override
public Connection getConnection() {
Connection connection = connectionPool
.remove(connectionPool.size() - 1);
usedConnections.add(connection);
return connection;
}
@Override
public boolean releaseConnection(Connection connection) {
connectionPool.add(connection);
return usedConnections.remove(connection);
}
private static Connection createConnection(
String url, String user, String password)
throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public int getSize() {
return connectionPool.size() + usedConnections.size();
}
// standard getters
}