views:

56

answers:

2

I have a java programs that gives me an error after working fine for a few hours ... these programs used to work fine on our earlier server which had windows server 2003 now we have upgraded to windows server 2008 with a higher configuration and newly installed SQL Server .Is there any db setting that i'm missing or is there any OS setting that i've missed out ??

the EXCEPTION i receive is :

Error::

org.apache.commons.dbcp.SQLNestedException:Cannot create PoolableConnectionFactory, cause: Network error

IOException: No buffer space available (maximum connections reached?): connect

A: 

Use a different DB connection pooling package like c3p0. also, check its compatibility with you JDBC driver.

Amit
A: 

I have a java programs that gives me an error after working fine for a few hours ...

IOException: No buffer space available (maximum connections reached?)

The JDBC code is likely not properly closing connections in the finally block of the try where the connection is been acquired. This way the connections will be kept open until the DB forcibly times-out and closes them. The timeout depends on the DB config used. Apparently the timeout was been relatively short in the previous machine and relatively long in the new machine. When the DB runs out of available connections because your application never closes them, then you will get an exception like that.

The following code examples illustrates the normal (basic) JDBC idiom for resource handling (note the code flow and the code comments):

public List<Entity> list() throws SQLException {
    // Declare resources.
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<Entity> entities = new ArrayList<Entity>();

    try {
        // Acquire resources.
        connection = database.getConnection();
        statement = connection.prepareStatement("SELECT id, name, value FROM entity");
        resultSet = statement.executeQuery();

        // Gather data.
        while (resultSet.next()) {
            Entity entity = new Entity(); 
            entity.setId(resultSet.getLong("id"));
            entity.setName(resultSet.getString("name"));
            entity.setValue(resultSet.getInteger("value"));
            entities.add(entity);
        }
    } finally {
        // Close resources in reversed order.
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    // Return data.
    return entities;
}
BalusC