Package play.db

Class DefaultDatabase

  • All Implemented Interfaces:
    Database

    public class DefaultDatabase
    extends Object
    implements Database
    Default delegating implementation of the database API.
    • Constructor Detail

      • DefaultDatabase

        public DefaultDatabase​(play.api.db.Database database)
      • DefaultDatabase

        public DefaultDatabase​(String name,
                               com.typesafe.config.Config configuration)
        Create a default HikariCP-backed database.
        Parameters:
        name - name for the db's underlying datasource
        configuration - the database's configuration
      • DefaultDatabase

        public DefaultDatabase​(String name,
                               Map<String,​? extends Object> config)
        Create a default HikariCP-backed database.
        Parameters:
        name - name for the db's underlying datasource
        config - the db's configuration
    • Method Detail

      • getName

        public String getName()
        Specified by:
        getName in interface Database
        Returns:
        the configuration name for this database.
      • getDataSource

        public DataSource getDataSource()
        Specified by:
        getDataSource in interface Database
        Returns:
        the underlying JDBC data source for this database.
      • getUrl

        public String getUrl()
        Specified by:
        getUrl in interface Database
        Returns:
        the JDBC connection URL this database, i.e. `jdbc:...` Normally retrieved via a connection.
      • getConnection

        public Connection getConnection()
        Description copied from interface: Database
        Get a JDBC connection from the underlying data source. Autocommit is enabled by default.

        Don't forget to release the connection at some point by calling close().

        Specified by:
        getConnection in interface Database
        Returns:
        a JDBC connection
      • getConnection

        public Connection getConnection​(boolean autocommit)
        Description copied from interface: Database
        Get a JDBC connection from the underlying data source.

        Don't forget to release the connection at some point by calling close().

        Specified by:
        getConnection in interface Database
        Parameters:
        autocommit - determines whether to autocommit the connection
        Returns:
        a JDBC connection
      • withConnection

        public void withConnection​(ConnectionRunnable block)
        Description copied from interface: Database
        Execute a block of code, providing a JDBC connection. The connection and all created statements are automatically released.
        Specified by:
        withConnection in interface Database
        Parameters:
        block - code to execute
      • withConnection

        public <A> A withConnection​(ConnectionCallable<A> block)
        Description copied from interface: Database
        Execute a block of code, providing a JDBC connection. The connection and all created statements are automatically released.
        Specified by:
        withConnection in interface Database
        Type Parameters:
        A - the return value's type
        Parameters:
        block - code to execute
        Returns:
        the result of the code block
      • withConnection

        public void withConnection​(boolean autocommit,
                                   ConnectionRunnable block)
        Description copied from interface: Database
        Execute a block of code, providing a JDBC connection. The connection and all created statements are automatically released.
        Specified by:
        withConnection in interface Database
        Parameters:
        autocommit - determines whether to autocommit the connection
        block - code to execute
      • withConnection

        public <A> A withConnection​(boolean autocommit,
                                    ConnectionCallable<A> block)
        Description copied from interface: Database
        Execute a block of code, providing a JDBC connection. The connection and all created statements are automatically released.
        Specified by:
        withConnection in interface Database
        Type Parameters:
        A - the return value's type
        Parameters:
        autocommit - determines whether to autocommit the connection
        block - code to execute
        Returns:
        the result of the code block
      • withTransaction

        public void withTransaction​(ConnectionRunnable block)
        Description copied from interface: Database
        Execute a block of code in the scope of a JDBC transaction. The connection and all created statements are automatically released. The transaction is automatically committed, unless an exception occurs.
        Specified by:
        withTransaction in interface Database
        Parameters:
        block - code to execute
      • withTransaction

        public void withTransaction​(TransactionIsolationLevel isolationLevel,
                                    ConnectionRunnable block)
        Description copied from interface: Database
        Execute a block of code in the scope of a JDBC transaction. The connection and all created statements are automatically released. The transaction is automatically committed, unless an exception occurs.
        Specified by:
        withTransaction in interface Database
        Parameters:
        isolationLevel - determines transaction isolation level
        block - code to execute
      • withTransaction

        public <A> A withTransaction​(ConnectionCallable<A> block)
        Description copied from interface: Database
        Execute a block of code in the scope of a JDBC transaction. The connection and all created statements are automatically released. The transaction is automatically committed, unless an exception occurs.
        Specified by:
        withTransaction in interface Database
        Type Parameters:
        A - the return value's type
        Parameters:
        block - code to execute
        Returns:
        the result of the code block
      • withTransaction

        public <A> A withTransaction​(TransactionIsolationLevel isolationLevel,
                                     ConnectionCallable<A> block)
        Description copied from interface: Database
        Execute a block of code in the scope of a JDBC transaction. The connection and all created statements are automatically released. The transaction is automatically committed, unless an exception occurs.
        Specified by:
        withTransaction in interface Database
        Type Parameters:
        A - the return value's type
        Parameters:
        isolationLevel - determines transaction isolation level
        block - code to execute
        Returns:
        the result of the code block
      • shutdown

        public void shutdown()
        Description copied from interface: Database
        Shutdown this database, closing the underlying data source.
        Specified by:
        shutdown in interface Database