Estou tendo problemas com o meu pool de conexões.
Quando tento acessar o sistema ele trava e não faz nada. O problema acontece quando a conexão fica muito tempo ociosa(geralmente de manhã). Estou usando C3p0.
Conexao.
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Conexao {
String usuario = "usuario";
String senha = "senha";
String banco = "banco";
String servidor = "localhost";
private static Conexao instancia;
private ComboPooledDataSource cpds;
private Conexao() throws SQLException {
cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://" + servidor + ":3306/" + banco);
cpds.setUser(usuario);
cpds.setPassword(senha);
cpds.setMinPoolSize(3);
cpds.setAcquireIncrement(3);
cpds.setMaxPoolSize(100);
cpds.setMaxIdleTime(1000);
cpds.setNumHelperThreads(5);
} catch (PropertyVetoException e) {
throw new SQLException("Erro na Conexao: " + e.getMessage());
} catch (Exception e) {
throw new SQLException("Erro ao fazer Backup " + e.getMessage());
}
}
public synchronized Connection getConexao() throws SQLException {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss");
System.out.println(df.format(new Date()));
System.out.println("Conexoes ativas: " + cpds.getNumConnections());
System.out.println("Conexoes Ociosas: " + cpds.getNumIdleConnections());
return cpds.getConnection();
} catch (SQLException ex) {
throw new SQLException(ex.getMessage());
}
}
static int i = 0;
public static Conexao getInstance() throws SQLException {
i++;
System.out.println("Quant: " + i);
if (instancia == null) {
instancia = new Conexao();
}
return instancia;
}
}
DAO
public class UsuarioDAO {
Connection conexao;
public UsuarioDAO() throws SQLException {
Conexao c = Conexao.getInstance();
conexao = c.getConexao();
}
public List<Usuario> listar(String ordem) throws SQLException {
List<Usuario> lista = new ArrayList<Usuario>();
String sql = "select * from usuario order by " + ordem;
try {
PreparedStatement ps = conexao.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
Usuario u;
while (rs.next()) {
u = new Usuario();
u.setCodigo(rs.getInt("usu_codigo"));
u.setNome(rs.getString("usu_nome"));
u.setLogin(rs.getString("usu_login"));
u.setCpf(rs.getString("usu_cpf"));
u.setCadastro(rs.getDate("usu_cadastro"));
lista.add(u);
}
} catch (SQLException e) {
throw new SQLException("Erro ao listar usuarios: " + e.getMessage());
} finally {
conexao.close();
}
return lista;
}
}
E chamo assim.
new UsuarioDAO().listar("usu_nome");
No log do tomcat fica mais ou menos assim
Quant: 79
2013/06/19 - 11:19:35 Ativas: 28 Ociosas: 3
Quant: 80
2013/06/19 - 11:19:35 Ativas: 28 Ociosas: 2
Quant: 81
2013/06/19 - 11:19:40 Ativas: 28 Ociosas: 0
Quant: 82
2013/06/19 - 11:19:35 Ativas: 31 Ociosas: 3
.
.
.
Quant: 300
2013/06/19 - 12:25:35 Ativas: 100 Ociosas: 0
Quant: 301
Quant: 302
Quant: 303
Quando as conexões ativas chegam a 100 o sistema trava e não consigo mais fazer nada.