Class FluentJdbc

  • All Implemented Interfaces:
    JdbcOperations

    public class FluentJdbc
    extends Object
    implements JdbcOperations
    A thread-safe JdbcOperations implementation.

    This class must be constructed with a DataSource that will be used as necessary to obtain connections to the database.

     import javax.sql.DataSource;
     import org.soulwing.jdbc.FluentJdbc;
    
     DataSource dataSource = ...  // typically injected or retrieved via JNDI
     FluentJdbc sqlTemplate = new FluentJdbc(dataSource);
     
    A single instance of this class that is constructed with an appropriate DataSource can be concurrently shared by an arbitrary number of application components.
    Author:
    Carl Harris
    • Constructor Detail

      • FluentJdbc

        public FluentJdbc​(DataSource dataSource)
        Constructs a new instance.
        Parameters:
        dataSource - data source that will be used to obtain connections to the database
      • FluentJdbc

        public FluentJdbc​(Connection connection)
        Constructs a new instance for use by a single thread.

        Use this constructor to create a facade instance that uses the given connection whenever a connection to the database is needed. This allows the facade to be used in conjunction with a framework such as Flyway that wants to explicit manage transaction state by providing a specific connection to each operation that needs one.

        Because an instance created via this constructor uses a single database connection, it should not be used by multiple concurrent threads.

        Parameters:
        connection - connection that will be used for all JDBC operations invoked using this facade instance
    • Method Detail

      • execute

        public void execute​(String sql)
        Executes a single SQL (DDL) statement.
        Specified by:
        execute in interface JdbcOperations
        Parameters:
        sql - the SQL statement to execute
      • execute

        public void execute​(SQLSource source)
        Executes a single SQL (DDL) statement.
        Specified by:
        execute in interface JdbcOperations
        Parameters:
        source - source for the SQL statement to execute
      • executeScript

        public void executeScript​(SQLSource source)
        Executes the sequence of SQL statements produced by the given source.
        Specified by:
        executeScript in interface JdbcOperations
        Parameters:
        source - source of the SQL statements to execute
      • queryForType

        public <T> JdbcQuery<T> queryForType​(Class<T> type)
        Creates a query operation for objects of a given type.
        Specified by:
        queryForType in interface JdbcOperations
        Parameters:
        type - subject type of the query
        Returns:
        a query that can be configured and executed to retreive rows from the database and return objects of type T.
      • update

        public JdbcUpdate update()
        Creates an update operation.
        Specified by:
        update in interface JdbcOperations
        Returns:
        an updater that can be configured and executed to update rows in the database.
      • call

        public JdbcCall call​(String sql)
        Creates a call operation.
        Specified by:
        call in interface JdbcOperations
        Parameters:
        sql - SQL call statement
        Returns:
        call operation
      • call

        public JdbcCall call​(SQLSource source)
        Creates a call operation.
        Specified by:
        call in interface JdbcOperations
        Parameters:
        source - source for the SQL call statement
        Returns:
        call operation;
      • setLogger

        public void setLogger​(JdbcLogger logger)
        Sets the logger to use for SQL statement logging.
        Parameters:
        logger - the logger to set (may be null to disable logging)
      • setLogger

        public void setLogger​(PrintWriter writer)
        Sets a print writer to use for SQL statement logging.
        Parameters:
        writer - the logger to set (may be null to disable logging)
      • setLogger

        public void setLogger​(PrintWriter writer,
                              boolean traceEnabled)
        Sets a print writer to use for SQL statement logging.
        Parameters:
        writer - the logger to set (may be null to disable logging)
        traceEnabled - flag indicating whether trace level logging should be used
      • setLogger

        public void setLogger​(PrintStream stream)
        Sets a print stream to use for SQL statement logging.
        Parameters:
        stream - the logger to set (may be null to disable logging)
      • setLogger

        public void setLogger​(PrintStream stream,
                              boolean traceEnabled)
        Sets a print stream to use for SQL statement logging.
        Parameters:
        stream - the logger to set (may be null to disable logging)
        traceEnabled - flag indicating whether trace level logging should be used
      • isAutoCommit

        public boolean isAutoCommit()
        Gets the autoCommit flag state.

        Returns:
        true if executeScript will use auto-commit mode
      • setAutoCommit

        public void setAutoCommit​(boolean autoCommit)
        Sets the autoCommit flag.

        This state of this flag is checked when executeScript is invoked. If true, all statements executed by the script will be auto-committed.

        In a managed transaction environment (e.g. in a Java EE application) setting this flag to true may cause script execution to fail.

        Parameters:
        autoCommit - the auto-commit flag state to set
      • isIgnoreErrors

        public boolean isIgnoreErrors()
        Gets the ignoreErrors flag state.
        Returns:
        true if executeScript will ignore errors at the statement level
      • setIgnoreErrors

        public void setIgnoreErrors​(boolean ignoreErrors)
        Sets the ignoreErrors flag.

        This state of this flag is checked when executeScript is invoked. If true, errors thrown by the execution of any given statement will be ignored.

        In general, ignoring errors is effective only when the auto-commit flag is also set to true.

        Parameters:
        ignoreErrors - the flag state to set