Rule Definition
Creating a connection to the database server is expensive. It is even more expensive if the server is located on another machine. Connection can take 30 to 50 ms depending on the platform.
Remediation
Use a pool of connections unless the DriverManager is the implementation of the connection pool itself.
In a J2EE container, it is recommended to use a JNDI DataSource provided by the container. Outside of a J2EE container, consider Apache's Jakarta Commons DBCP or C3P0. Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full connection pool beans, supporting the same basic properties as this class plus specific settings (such as minimal/maximal pool size etc)..
Violation Code Sample
Connection con = DriverManager.getConnection(URL, user, password); // VIOLATION
Fixed Code Sample
import org.apache.commons.dbcp.BasicDataSource;
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl("jdbc:oracle:thin:scott/tiger@myhost:1521:mysid");
Connection con = ds.getConnection();
Reference
http://www.precisejava.com/javaperf/j2ee/EJB.htm#EJB142
http://www.webdevelopersjournal.com/columns/connection_pool.html
http://commons.apache.org/dbcp/
http://sourceforge.net/projects/c3p0
Related Technologies
JEE
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.