Module aeonics.core

Class Database.PooledConnection

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Enclosing class:
    Database

    public static class Database.PooledConnection
    extends java.lang.Object
    implements java.lang.AutoCloseable
    This class is a wrapper around a java.sql.Connection class to be used with the pooled Database.Type.
    • Constructor Summary

      Constructors 
      Constructor Description
      PooledConnection​(Database.Type pool, java.sql.Connection connection)
      Creates a new connection attached to the specified pool
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Return this connection to the pool after a query operation is complete.
      Data columns​(java.lang.String table)
      Returns the list of columns in the specified table.
      void destroy()
      Closes the underlying database connection.
      boolean isValid()
      Returns true if the underlying database connection is ready to perform a query
      Data query​(java.lang.String sql, java.lang.Object... params)
      Performs a query.
      Data tables()
      Returns the list of tables that can be accessed from this connection.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PooledConnection

        public PooledConnection​(Database.Type pool,
                                java.sql.Connection connection)
        Creates a new connection attached to the specified pool
        Parameters:
        pool - the connection pool
        connection - the underlying jdbc connection
    • Method Detail

      • destroy

        public void destroy()
        Closes the underlying database connection. Do not confuse this method with the close() method that simply returns this object to the connection pool.
      • isValid

        public boolean isValid()
        Returns true if the underlying database connection is ready to perform a query
        Returns:
        true if the underlying database connection is ready to perform a query
      • query

        public Data query​(java.lang.String sql,
                          java.lang.Object... params)
                   throws java.sql.SQLException
        Performs a query.

        CAUTION : use in a try...with statement or do not forget to close() after use to return to the pool The returned column names should ALWAYS be lower case.

        The parameters provided for substitution will be provided to the underlying connection using PreparedStatement.setObject(int, Object) so that the compatibility with String values is maximized. (see JDBC 4.3 Specification (JSR 221) Table B.5 for Conversions Performed by setObject and setNull Between Java Object Types and Target JDBC Types)

        Parameters:
        sql - the parameterized query to perform
        params - the parameters to substitute.
        Returns:
        the resulset (Data list), the inserted primary key (Data object), or the number of affected rows depending on the case
        Throws:
        java.sql.SQLException - if an error occurs at the database level
      • close

        public void close()
        Return this connection to the pool after a query operation is complete.

        You should not need to call this method manually if used in a try...with statement.

        Specified by:
        close in interface java.lang.AutoCloseable
      • tables

        public Data tables()
                    throws java.sql.SQLException
        Returns the list of tables that can be accessed from this connection.

        The returned data is a list of table descriptions that include:

        • name: the table name
        • schema: the table schema
        • database: the table database (or catalog in JDBC terms)
        Returns:
        the list of tables
        Throws:
        java.sql.SQLException - if an error happens
        See Also:
        DatabaseMetaData.getTables(String, String, String, String[])
      • columns

        public Data columns​(java.lang.String table)
                     throws java.sql.SQLException
        Returns the list of columns in the specified table.

        The returned data is a list of table descriptions that include:

        • name: the column name
        • size: the column data type size
        • auto: whether or not the column has a default or auto-increment value
        • null: whether or not the column accepts null values
        • type: the JDBC data type as a string
        • primary: whether or not the column is part of a primary key
        The information returned by this method is not exhaustive and may lack details for a complete automatic structure discovery. However it should be sufficient for most basic applications.
        Parameters:
        table - the name of the table
        Returns:
        the list of columns
        Throws:
        java.sql.SQLException - if an error happens
        See Also:
        DatabaseMetaData.getColumns(String, String, String, String), JDBCType