|
Play Framework/Scala example source code file (DB.java)
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 examplesHere 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 |
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.