title: Basic DB connection pool with Java and Tomcat 7
body: <p>I'm trying to create my first connection pool. I'm creating a Java web aplication with Tomcat 7 and a MySQL DB, and I'd like to create the simplest connection pool possible.
  I've taken a look at several tutorials but it's not really clear for me, so I'd like you to confirm if I'm doing well.</p>
  
  <p>I've written the following class as a connection pool manager:</p>
  
  <pre><code>package dao.mysql;
  
  import java.sql.Connection;
  import java.sql.SQLException;
  import org.apache.tomcat.jdbc.pool.DataSource;
  import org.apache.tomcat.jdbc.pool.PoolProperties;
  
  public class MySQLConnectionPool {
  
      private static DataSource datasource;
      private static String driver = "com.mysql.jdbc.Driver";
      private static String url = "jdbc:mysql://localhost:3306/mydb";
      private static String username = "user";
      private static String password = "password";
  
      public MySQLConnectionPool() {
          datasource = new DataSource(configurePoolProperties(driver, url, username, password));
      }
  
      private PoolProperties configurePoolProperties(String driver, String url, String username, String password) {
          PoolProperties properties = new PoolProperties();
          properties.setDriverClassName(driver);
          properties.setUrl(url);
          properties.setUsername(username);
          properties.setPassword(password);
          return properties;
      } 
  
      public static synchronized Connection getConnection() {
          Connection connection = null;
          try {
              connection = datasource.getConnection();
          } catch (SQLException ex) {
              System.out.println("Error while getting a connection from the pool! \nSQL state:" + ex.getSQLState() + "\nMESSAGE" + ex.getMessage());
          }
          return connection;
      }
  }
  </code></pre>
  
  <p>I'm not sure about the <strong>static</strong> properties nor the <strong>synchronized</strong>.</p>
  
  <p>And I'm not sure about the "client" classes of the pool. I understand they have only to get a connection using</p>
  
  <pre><code>Connection con = MySQLConnectionPool.getConnection();
  </code></pre>
  
  <p>and finally close this connection using</p>
  
  <pre><code>con.close();
  </code></pre>
  
  <p>And that's it?
  And also, is there any simpler or better way to do this?</p>
  
  <p>Thanks very much!</p>