Question

I am experimenting with BoneCP as a connection pooling alternative to DBCP. I added the bonecp-0.8.0rc-1.jar to my runtime classpath, and copy-n-pasted the code from their example page into my test driver:

Connection connection = null;
BoneCP connectionPool;
try {
    Class.forName(config.getDatabaseLogger().getJDBCDriver());

    BoneCPConfig boneConfig = configureBoneCP(config);

    connectionPool = new BoneCP(boneConfig);

    connection = connectionPool.getConnection();    // fetch a connection
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

On the line that reads:

connectionPool = new BoneCP(boneConfig);

Eclipse (Juno) is giving me a compiler warning:

Resource leak: 'connectionPool' is never closed

It then gives me an option to "Add @SuppressWarnings 'resource' to newConnectionManager" (newConnectionManager() references the class this is contained in).

How is this possible?!?! How does Eclipse know anything about a BoneCP object, and how does it know that it must be closed? Is this some kind of "custom warning" that ships with the BoneCP library? If so, how could I add/annotate code to my own projects to help warn downstream developers that they aren't using my libraries correctly? Thanks in advance!

Was it helpful?

Solution

The warning is caused by this statement:

connection = connectionPool.getConnection();

The method getConnection() returns a object of type java.sql.Connection which implements java.lang.AutoCloseable since Java 7. Eclipse generates this warning when it can not determine whether a resource which implements either java.io.Closeable or java.lang.AutoCloseable gets closed by all possible code paths.

OTHER TIPS

How does Eclipse know anything about a BoneCP object, 
                                  and how does it know that it must be closed?

Docs of eclipse says

Classes implementing the interface java.io.Closeable (since JDK 1.5) and java.lang.AutoCloseable (since JDK 1.7) are considered to represent external resources, which should be closed using method close(), when they are no longer needed.

The Eclipse Java compiler is able to analyze whether code using such types adheres to this policy.

objects do not directly represent an operating system resource. If the wrapped resource is closed, the wrapper doesn't need closing. Conversely, if a wrapper is closed this will include closing of the wrapped resource. The analysis has a second white list for detecting wrapper resources, and will recognize whether the underlying actual resource will be closed directly or indirectly via the wrapper. Either one suffices to silence warnings regarding resource leaks. The white list contains classes from java.io, java.util.zip, java.security, java.beans and java.sound.sampled.

Your BoneCP classe implements the interface java.io.Serializable ,this might be the reason.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top