alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (DB.java)

This example Play Framework source code file (DB.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

a, abstractfunction1, api, application, connection, connectioncallable, database, datasource, db, play, play framework, runtime

The DB.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.db;

import java.sql.Connection;
import javax.sql.DataSource;

import scala.runtime.AbstractFunction1;

import play.api.Application;

/**
 * Provides a high-level API for getting JDBC connections.
 */
public class DB {
    
    /**
     * Returns the default datasource.
     */
    public static DataSource getDataSource() {
        return getDataSource("default");
    }
    
    /**
     * Returns specified database.
     *
     * @param name Datasource name
     */
    public static DataSource getDataSource(String name) {
        return play.api.db.DB.
            getDataSource(name, play.api.Play.unsafeApplication());
    }
    
    /**
     * Returns a connection from the default datasource, 
     * with auto-commit enabled.
     */
    public static Connection getConnection() {
        return getConnection("default");
    }
    
    /**
     * Returns a connection from the default datasource, 
     * with the specified auto-commit setting.
     */
    public static Connection getConnection(boolean autocommit) {
        return getConnection("default", autocommit);
    }
    
    /**
     * Returns a connection from any datasource, with auto-commit enabled.
     *
     * @param name Datasource name
     */
    public static Connection getConnection(String name) {
        return getConnection(name, true);
    }
    
    /**
     * Get a connection from any datasource, 
     * with the specified auto-commit setting.
     *
     * @param name Datasource name
     * @param autocommit Auto-commit setting
     */
    public static Connection getConnection(String name, boolean autocommit) {
        return play.api.db.DB.
            getConnection(name, autocommit, 
                          play.api.Play.unsafeApplication());
    }
    
    /**
     * Executes a block of code, providing a JDBC connection. 
     * The connection and all created statements are automatically released.
     *
     * @param name Datasource name
     * @param block Code block to execute
     * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>)
     */
    public static <A> A withConnection(String name, 
                                       ConnectionCallable<A> block,
                                       Application application) {

        return play.api.db.DB.
            withConnection(name, connectionFunction(block), application);

    }

    /**
     * Execute a block of code, providing a JDBC connection. 
     * The connection and all created statements are automatically released.
     *
     * @param block Code block to execute
     * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>)
     */
    public static <A> A withConnection(ConnectionCallable<A> block, 
                                       Application application) {

        return play.api.db.DB.
            withConnection(connectionFunction(block), application);
    }

    /**
     * Executes a block of code, providing a JDBC connection. 
     * The connection and all created statements are automatically released.
     *
     * @param name Datasource name
     * @param block Code block to execute
     */
    public static <A> A withConnection(String name, ConnectionCallable<A> block) {
        return play.api.db.DB.withConnection(name, connectionFunction(block), 
                                             play.api.Play.unsafeApplication());

    }

    /**
     * Execute a block of code, providing a JDBC connection. 
     * The connection and all created statements are automatically released.
     *
     * @param block Code block to execute
     */
    public static <A> A withConnection(ConnectionCallable<A> block) {
        return play.api.db.DB.withConnection(connectionFunction(block), 
                                             play.api.Play.unsafeApplication());
    }

    /**
     * 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.
     *
     * @param name Datasource name
     * @param block Code block to execute
     * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>)
     */
    public static <A> A withTransaction(String name, 
                                        ConnectionCallable<A> block,
                                        Application application) {

        return play.api.db.DB.
            withTransaction(name, connectionFunction(block), application);
    }

    /**
     * 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.
     *
     * @param block Code block to execute
     * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>)
     */
    public static <A> A withTransaction(ConnectionCallable<A> block, 
                                        Application application) {

        return play.api.db.DB.
            withTransaction(connectionFunction(block), application);
        
    }

    /**
     * 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.
     *
     * @param name Datasource name
     * @param block Code block to execute
     */
    public static <A> A withTransaction(String name, 
                                        ConnectionCallable<A> block) {

        return play.api.db.DB.
            withTransaction(name, connectionFunction(block), 
                            play.api.Play.unsafeApplication());
    }

    /**
     * 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.
     *
     * @param block Code block to execute
     */
    public static <A> A withTransaction(ConnectionCallable<A> block) {

        return play.api.db.DB.
            withTransaction(connectionFunction(block), 
                            play.api.Play.unsafeApplication());
        
    }

    /** Returns a function wrapper from Java callable. */
    private static final <A> AbstractFunction1<Connection,A> connectionFunction(final ConnectionCallable<A> block) {
        return new AbstractFunction1<Connection,A>() {
            public A apply(Connection con) { return block.call(con); }
        };
    }
}

/**
 * Similar to java.util.concurrent.Callable with a connection as argument.
 * Usable from vanilla Java 6+, or as lambda as it's a functionnal interface.
 *
 * Vanilla Java:
 * <code>
 * new ConnectionCallable<A>() {
 *   public A call(Connection con) { ... }
 * }
 * </code>
 *
 * Java Lambda:
 * <code>(Connection con) -> ...</code>
 */
interface ConnectionCallable<A> {
    public A call(Connection con);
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework DB.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.