Skip to content

Second Tutorial Part 3: Connection Management

Laurent Hasson edited this page Dec 3, 2019 · 2 revisions
Previous Main Next
<-- Part 2 Main Part 4 -->

Tilda wraps the standard JDBC Connection objects and adds a few extras. We also reuse the Apache Commons DBCP2 library to do pooling. The main classes are:

package tilda_tutorial;

import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import tilda.db.Connection;
import tilda.db.ConnectionPool;

public class Sample
  {

    protected static final Logger LOG = LogManager.getLogger(Sample.class.getName());

    public static void main(String[] args)
      {

        // The tilda.db.Connection class wraps the standard JDBC Connection
        // class and is expected to behave the same way while offering 
        // additional capabilities.
        Connection C = null;

        try
          {
            // The tilda.db.ConnectionPool class manages initialization
            // time aspects of Tilda, and builds on top of 
            // org.apache.commons.dbcp2.BasicDataSource.
            C = ConnectionPool.get("MAIN");

            LOG.info("===== TEST 1 ============================================");

            // Calling our logic and committing or rollbacking as appropriate.   
            if (Test1(C) == true)
              C.commit();
            else
              C.rollback();
          }
        catch (Exception E)
          {
            LOG.error("An exception occurred\n", E);
          }
        finally
          {
            if (C != null)
              try
                {
                  // Resource cleanup with a rollback by default.
                  C.rollback();

                  // The connection is managed by th econnection pool, so 
                  // close() implicitly returns the connection to the pool.
                  C.close();
                }
              catch (SQLException E)
                {
                }
          }

      }

    private static boolean Test1(Connection C)
    throws Exception
      {
        return false;
      }

  }
Previous Main Next
<-- Part 2 Main Part 4 -->
Clone this wiki locally