JDBC lint helps Java programmers write correct and efficient code when using the JDBC API. JDBC lint requires Java 6 and has no other run-time dependencies. Andrew Gaul at Maginatics [email protected] originally wrote JDBC lint.
JDBC lint warns about many different conditions, configured via the following properties:
- com.maginatics.jdbclint.blob.double_free
- com.maginatics.jdbclint.blob.missing_free
- com.maginatics.jdbclint.connection.double_close
- com.maginatics.jdbclint.connection.missing_close
- com.maginatics.jdbclint.connection.missing_commit_or_rollback
- com.maginatics.jdbclint.connection.missing_prepare_statement
- com.maginatics.jdbclint.preparedstatement.double_close
- com.maginatics.jdbclint.preparedstatement.missing_close
- com.maginatics.jdbclint.preparedstatement.missing_execute
- com.maginatics.jdbclint.preparedstatement.missing_execute_batch
- com.maginatics.jdbclint.resultset.double_close
- com.maginatics.jdbclint.resultset.missing_close
- com.maginatics.jdbclint.resultset.unread_column
- com.maginatics.jdbclint.statement.double_close
- com.maginatics.jdbclint.statement.missing_close
- com.maginatics.jdbclint.statement.missing_execute
- com.maginatics.jdbclint.statement.missing_execute_batch
JDBC lint enables all warnings by default and users can disable individual ones by setting the corresponding property to false.
To make use of JDBC lint in an Apache Maven based project, add it as a dependency:
<dependency>
<groupId>com.maginatics</groupId>
<artifactId>jdbclint</artifactId>
<version>0.3.0</version>
</dependency>
Users can enable it by wrapping their Connection or DataSource objects and configure it via Java properties. For example:
import com.maginatics.jdbclint.ConnectionProxy;
...
Connection connection = DriverManager.getConnection(...);
connection = ConnectionProxy.newInstance(connection, new Properties());
connection.close();
connection.close(); // reports error and optionally throws exception
JDBC lint reports any errors to stderr by default and users can redirect this to a file by setting com.maginatics.jdbclint.log_file . Users can also throw a RuntimeException, SQLException, or exit on errors by setting com.maginatics.jdbclint.fail_method to throw_runtime_exception, throw_sql_exception, or exit, respectively.
JDBC lint implements its checks by wrapping concrete implementations like Connection with dynamic proxy classes. This allows JDBC lint to add its checks before and after the concrete method invocation while preserving all behaviors of the original class. Some checks like warning about missing calls to close require use of finalization which depends on the behavior of Java garbage collection.
JDBC has significant complexity and other tools can help write correct code. Specifically FindBugs can detect failures to close SQL resources and Java 7 try-with-resources can prevent the same mistakes. JDBI offers an annotation-based approach to writing SQL which avoids some kinds of errors. jOOQ offers a fluent API that hides most interaction with JDBC from client code. JDBC lint can work in conjunction with all of these tools.
Copyright (C) 2012-2013 Maginatics, Inc.
Licensed under the Apache License, Version 2.0