001package io.ebean; 002 003import io.ebean.config.ContainerConfig; 004import io.ebean.config.DatabaseConfig; 005 006/** 007 * Creates Database instances. 008 * <p> 009 * This uses either DatabaseConfig or properties in the application.properties file to 010 * configure and create a Database instance. 011 * </p> 012 * <p> 013 * The Database instance can either be registered with the DB singleton or 014 * not. The DB singleton effectively holds a map of Database by a name. 015 * If the Database is registered with the DB singleton you can retrieve it 016 * later via {@link DB#byName(String)}. 017 * </p> 018 * <p> 019 * One Database can be nominated as the 'default/primary' Database. Many 020 * methods on the DB singleton such as {@link DB#find(Class)} are just a 021 * convenient way of using the 'default/primary' Database. 022 * </p> 023 */ 024public class DatabaseFactory { 025 026 /** 027 * Initialise the container with clustering configuration. 028 * <p> 029 * Call this prior to creating any Database instances or alternatively set the 030 * ContainerConfig on the ServerConfig when creating the first Database instance. 031 */ 032 public static synchronized void initialiseContainer(ContainerConfig containerConfig) { 033 EbeanServerFactory.initialiseContainer(containerConfig); 034 } 035 036 /** 037 * Create using ebean.properties to configure the server. 038 */ 039 public static synchronized Database create(String name) { 040 return EbeanServerFactory.create(name); 041 } 042 043 /** 044 * Create using the ServerConfig object to configure the server. 045 */ 046 public static synchronized Database create(DatabaseConfig config) { 047 return EbeanServerFactory.create(config); 048 } 049 050 /** 051 * Create using the ServerConfig additionally specifying a classLoader to use as the context class loader. 052 */ 053 public static synchronized Database createWithContextClassLoader(DatabaseConfig config, ClassLoader classLoader) { 054 return EbeanServerFactory.createWithContextClassLoader(config, classLoader); 055 } 056 057 /** 058 * Shutdown gracefully all EbeanServers cleaning up any resources as required. 059 * <p> 060 * This is typically invoked via JVM shutdown hook and not explicitly called. 061 * </p> 062 */ 063 public static synchronized void shutdown() { 064 EbeanServerFactory.shutdown(); 065 } 066 067}