I have done a socket programming using bonecp as the database pooling for mysql. Suddenly I notice a lot of socket connection made but then after a few hours I got too many open files. I have set the timeout but somehow it did not timeout I don't know why. Why did the timeout did not trigger or worked in this scenario? I need to know is this due to database pooling or socket problem?
Below is snippet of my code. In my code I notice in the logs the connection is made and stops at this line-
System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection();
There is possiblity it does not get the connection from pool but there is no error or any socket connection timeout. Is there any solution for this? How over come this or resolve to that is the pool is full to throw some exception.
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
BufferedWriter writer1 = null;
Connection dbconn1 = null;
public void run() { // etc
writer1 = null;
String message="";
BufferedReader reader1 = null;
try {
writer1 = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
reader1 = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
receivedSocketConn1.setSoTimeout(60000);
int nextChar=0;
while ((nextChar=reader1.read()) != -1) {
message += (char) nextChar;
if (nextChar == '*'){
try{
System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
System.out.println("\n\n Checking db connection status "+dbconn1.isClosed());
if ((dbconn1 == null) || dbconn1.isClosed()) {
System.out.println("\n\n db connection status is closed");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
//other codes follow here.
}
}
catch (SQLException ex){
System.out.println("Error SQL Exception : "+ex.toString());
ex.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
catch (Exception e){
System.out.println("\nSQL Error here :");
e.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
finally{
try {
if ( dbconn1 != null ) {
dbconn1.close();
System.out.println("\n\n dbConn1 is being closed");
}
}
catch(SQLException ex){
System.out.println("SQLException has been caught for dbConn1 close");
ex.printStackTrace();
}
}
}
}
}
catch (SocketTimeoutException ex){
System.out.println("SocketTimeoutException has been caught ");
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println("IOException has been caught ");
ex.printStackTrace();
}
catch (Exception ex) {
System.out.println("Exception has been caught");
ex.printStackTrace(System.out);
}
finally{
try {
if (writer1 != null ) {
writer1.close();
}
}
catch(IOException ex){
System.out.println("IOException has been caught for finally");
ex.printStackTrace(System.out);
}
}
}
public static void main(String[] args) {
try {
// setup the connection pool
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/****"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername("***");
config.setPassword("***");
config.setMinConnectionsPerPartition(5);
config.setMaxConnectionsPerPartition(40);
config.setPartitionCount(1);
connectionPool = new BoneCP(config); // setup the connection pool
}
catch (SQLException e) {
e.printStackTrace(System.out);
}
try
{
final ServerSocket serverSocketConn = new ServerSocket(8888);
while (true)
{
try
{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e)
{
System.out.println("Socket Accepting has been caught);
e.printStackTrace(System.out);
}
}
}
catch (Exception e)
{
System.out.println("Socket Conn has been caught );
e.printStackTrace(System.out);
}
}