1 /* 2 * Copyright 2006-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package net.sourceforge.domian.repository; 17 18 19 import static java.lang.Boolean.FALSE; 20 import static java.lang.Boolean.TRUE; 21 import java.util.HashMap; 22 import java.util.List; 23 import java.util.Map; 24 25 import static org.apache.commons.lang.StringUtils.isNotBlank; 26 import static org.apache.commons.lang.SystemUtils.FILE_SEPARATOR; 27 import org.slf4j.Logger; 28 import static org.slf4j.LoggerFactory.getLogger; 29 30 31 enum Rdbms { 32 33 // TODO: add all Hibernate-supported RDBMSs - use the "supported" field to warn people of possible dubious behaviour... 34 H2("H2", "http://www.h2database.com", 35 TRUE, 36 "org.hibernate.dialect.H2Dialect", "org.h2.Driver", 37 "jdbc:h2:tcp:", 38 //"", ""), // Server mode for developing/debugging 39 "jdbc:h2:", ";DB_CLOSE_ON_EXIT=TRUE"), 40 41 HSQLDB("HSQLDB", "http://hsqldb.org", 42 FALSE, 43 "org.hibernate.dialect.HSQLDialect", "org.hsqldb.jdbcDriver", 44 "jdbc:hsqldb:hsql:", 45 //"", ""), // Server mode for developing/debugging 46 "jdbc:hsqldb:file:", ""), 47 48 MYSQL_INNODB("MySQL", "http://www.mysql.com", 49 FALSE, 50 "org.hibernate.dialect.MySQLInnoDBDialect", "", 51 "", 52 "", ""), 53 54 ORACLE_10G("Oracle 10g", "http://www.oracle.com/technology/products/database/xe/index.html", 55 FALSE, 56 "org.hibernate.dialect.Oracle10gDialect", "", 57 "", 58 "", ""); 59 60 private static final Logger log = getLogger(Rdbms.class); 61 62 private final String officialName; 63 private final Boolean supported; 64 private final String officialWebUrl; 65 66 private final String hibernateDialectClassName; 67 private final String driverClassName; 68 69 private final String jdbcConnectionUrlPrefix; 70 71 private final String embeddedJdbcConnectionUrlPrefix; 72 private final String embeddedJdbcConnectionUrlPostfix; 73 74 private Rdbms(String officialName, 75 String officialWebUrl, 76 Boolean supported, 77 String hibernateDialectClassName, 78 String driverClassName, 79 String jdbcConnectionUrlPrefix, 80 String embeddedJdbcConnectionUrlPrefix, 81 String embeddedJdbcConnectionUrlPostfix) { 82 this.officialName = officialName; 83 this.officialWebUrl = officialWebUrl; 84 this.supported = supported; 85 this.hibernateDialectClassName = hibernateDialectClassName; 86 this.driverClassName = driverClassName; 87 this.jdbcConnectionUrlPrefix = jdbcConnectionUrlPrefix; 88 this.embeddedJdbcConnectionUrlPrefix = embeddedJdbcConnectionUrlPrefix; 89 this.embeddedJdbcConnectionUrlPostfix = embeddedJdbcConnectionUrlPostfix; 90 } 91 92 List<String> hibernateConfigKeyList; 93 94 String getName() { 95 return this.officialName; 96 } 97 98 String getOfficialName() { 99 return getName(); 100 } 101 102 String getUnixName() { 103 return getOfficialName().toLowerCase(); 104 } 105 106 Boolean isSupported() { 107 return this.supported; 108 } 109 110 String getDefaultRepositoryRootDirectoryString(final String defaultDomianRootPath) { 111 return defaultDomianRootPath + FILE_SEPARATOR + ".hibernate-" + getUnixName() + "-repository"; 112 } 113 114 String getJdbcConnectionUrl(final String rdbmsServerName, final String repositoryDirectoryString, final String repositoryId) { 115 if (isNotBlank(this.embeddedJdbcConnectionUrlPrefix)) { 116 return this.embeddedJdbcConnectionUrlPrefix + repositoryDirectoryString + FILE_SEPARATOR + repositoryId + this.embeddedJdbcConnectionUrlPostfix; 117 } 118 // Server mode fallback 119 return this.jdbcConnectionUrlPrefix + "//" + rdbmsServerName + "/" + repositoryDirectoryString + FILE_SEPARATOR + repositoryId; 120 } 121 122 123 Map getConfiguration(final String rdbmsServerName, 124 final String repositoryDirectoryString, 125 final String repositoryId, 126 final Map<String, String> configurationMap) { 127 128 final Map<String, String> entityManagerFactoryProperties = new HashMap<String, String>(); 129 130 // **** Hibernate properties (Domian standards - should not be overridden (WARN message if so)) **** 131 entityManagerFactoryProperties.put("hibernate.dialect", this.hibernateDialectClassName); 132 entityManagerFactoryProperties.put("hibernate.connection.driver_class", this.driverClassName); 133 134 // TODO: how to make this happen? is it even possible in JPA 1.0... 135 //entityManagerFactoryProperties.put("hibernate.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy"); 136 137 // **** Hibernate properties (Domian defaults - ment to be overridden at will (INFO message if so)) **** 138 // Default JDBC URL building (embedded mode, with server mode fallback) 139 entityManagerFactoryProperties.put("hibernate.connection.url", this.getJdbcConnectionUrl(rdbmsServerName, repositoryDirectoryString, repositoryId)); 140 141 // Default Domian schema creation/maintenance (automatic updating of database schema based on ORM config) 142 entityManagerFactoryProperties.put("hibernate.hbm2ddl.auto", "update"); 143 //entityManagerFactoryProperties.put("hibernate.hbm2ddl.auto", "create"); // Development setting... 144 145 entityManagerFactoryProperties.put("hibernate.max_fetch_depth", "5"); 146 //entityManagerFactoryProperties.put("hibernate.default_batch_fetch_size", "4"); // Sets a default size for Hibernate batch fetching of associations. e.g. recommended values 4, 8, 16 147 148 //entityManagerFactoryProperties.put("hibernate.connection.pool_size", "1"); 149 150 entityManagerFactoryProperties.put("hibernate.c3p0.min_size", "1"); 151 entityManagerFactoryProperties.put("hibernate.c3p0.max_size", "1"); 152 entityManagerFactoryProperties.put("hibernate.c3p0.timeout", "1800"); 153 entityManagerFactoryProperties.put("hibernate.c3p0.max_statements", "50"); 154 155 entityManagerFactoryProperties.put("hibernate.show_sql", "false"); 156 entityManagerFactoryProperties.put("hibernate.format_sql", "false"); 157 //entityManagerFactoryProperties.put("hibernate.default_schema", "false"); // Qualify unqualified table names with the given schema/tablespace in generated SQL. e.g. SCHEMA_NAME 158 //entityManagerFactoryProperties.put("hibernate.default_catalog", "false"); // Qualifies unqualified table names with the given catalog in generated SQL. e.g. CATALOG_NAME 159 160 //entityManagerFactoryProperties.put("hibernate.default_entity_mode", "pojo"); // Sets a default mode for entity representation for all sessions opened from this SessionFactory 161 //entityManagerFactoryProperties.put("hibernate.order_updates", "true"); // Forces Hibernate to order SQL updates by the primary key value of the items being updated. This will result in fewer transaction deadlocks in highly concurrent systems. e.g. true | false 162 //entityManagerFactoryProperties.put("hibernate.generate_statistics", "true"); // If enabled, Hibernate will collect statistics useful for performance tuning. e.g. true | false 163 //entityManagerFactoryProperties.put("hibernate.use_identifier_rollback", "true"); // If enabled, generated identifier properties will be reset to default values when objects are deleted. e.g. true | false 164 //entityManagerFactoryProperties.put("hibernate.use_sql_comments", "true"); // If turned on, Hibernate will generate comments inside the SQL, for easier debugging, defaults to false. 165 166 167 // **** Hibernate JDBC and Connection Properties **** 168 //entityManagerFactoryProperties.put("hibernate.jdbc.fetch_size", "100"); // A non-zero value determines the JDBC fetch size (calls Statement.setFetchSize()). 169 //entityManagerFactoryProperties.put("hibernate.jdbc.batch_size", "10"); // A non-zero value enables use of JDBC2 batch updates by Hibernate. e.g. recommended values between 5 and 30 170 //entityManagerFactoryProperties.put("hibernate.jdbc.batch_versioned_data", "true"); // Set this property to true if your JDBC driver returns correct row counts from executeBatch(). It is usually safe to turn this option on. Hibernate will then use batched DML for automatically versioned data. Defaults to false. 171 //entityManagerFactoryProperties.put("hibernate.jdbc.use_scrollable_resultset", ""); // Enables use of JDBC2 scrollable resultsets by Hibernate. This property is only necessary when using user-supplied JDBC connections. Hibernate uses connection metadata otherwise. e.g. true | false 172 //entityManagerFactoryProperties.put("hibernate.jdbc.use_streams_for_binary", ""); // Use streams when writing/reading binary or serializable types to/from JDBC. *system-level property* e.g. true | false 173 //entityManagerFactoryProperties.put("hibernate.jdbc.use_get_generated_keys", ""); // Enables use of JDBC3 PreparedStatement.getGeneratedKeys() to retrieve natively generated keys after insert. Requires JDBC3+ driver and JRE1.4+, set to false if your driver has problems with the Hibernate identifier generators. By default, it tries to determine the driver capabilities using connection metadata. e.g true|false 174 //entityManagerFactoryProperties.put("hibernate.connection.provider_class", ""); // The classname of a custom org.hibernate.connection.ConnectionProvider which provides JDBC connections to Hibernate. e.g. classname.of.ConnectionProvider 175 //entityManagerFactoryProperties.put("hibernate.connection.isolation", "8"); // Sets the JDBC transaction isolation level. Check java.sql.Connection for meaningful values 176 //entityManagerFactoryProperties.put("hibernate.connection.autocommit"; ""); // Enables autocommit for JDBC pooled connections (it is not recommended). e.g. true | false 177 //entityManagerFactoryProperties.put("hibernate.connection.release_mode", "after_transaction"); // Specifies when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, use after_statement to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using after_transaction. auto will choose after_statement for the JTA and CMT transaction strategies and after_transaction for the JDBC transaction strategy. e.g. auto (default) | on_close | after_transaction | after_statement 178 //entityManagerFactoryProperties.put("hibernate.connection.<propertyName>", "..."); // Pass the JDBC property <propertyName> to DriverManager.getConnection(). 179 //entityManagerFactoryProperties.put("hibernate.jndi.<propertyName>", "..."); // Pass the property <propertyName> to the JNDI InitialContextFactory. 180 181 // **** Hibernate Cache Properties **** 182 //entityManagerFactoryProperties.put("hibernate.cache.provider_class", ""); // The classname of a custom CacheProvider. e.g. classname.of.CacheProvider 183 //entityManagerFactoryProperties.put("hibernate.cache.use_minimal_puts", ""); // Optimizes second-level cache operation to minimize writes, at the cost of more frequent reads. This setting is most useful for clustered caches and, in Hibernate3, is enabled by default for clustered cache implementations. e.g. true|false 184 entityManagerFactoryProperties.put("hibernate.cache.use_query_cache", "false"); // Enables the query cache. Individual queries still have to be set cachable. e.g. true|false 185 entityManagerFactoryProperties.put("hibernate.cache.use_second_level_cache", "false"); // Can be used to completely disable the second level cache, which is enabled by default for classes which specify a <cache> mapping. e.g. true|false 186 //entityManagerFactoryProperties.put("hibernate.cache.query_cache_factory", ""); // The classname of a custom QueryCache interface, defaults to the built-in StandardQueryCache. e.g. classname.of.QueryCache 187 //entityManagerFactoryProperties.put("hibernate.cache.region_prefix", ""); // A prefix to use for second-level cache region names. e.g. prefix 188 //entityManagerFactoryProperties.put("hibernate.cache.use_structured_entries", "false"); // Forces Hibernate to store data in the second-level cache in a more human-friendly format. e.g. true|false 189 190 // **** Hibernate Transaction Properties **** 191 //entityManagerFactoryProperties.put("hibernate.transaction.factory_class", ""); // The classname of a TransactionFactory to use with Hibernate Transaction API (defaults to JDBCTransactionFactory). e.g. classname.of.TransactionFactory 192 //entityManagerFactoryProperties.put("jta.UserTransaction", ""); // A JNDI name used by JTATransactionFactory to obtain the JTA UserTransaction from the application server. e.g. jndi/composite/name 193 //entityManagerFactoryProperties.put("hibernate.transaction.manager_lookup_class", ""); // The classname of a TransactionManagerLookup. It is required when JVM-level caching is enabled or when using hilo generator in a JTA environment. e.g. classname.of.TransactionManagerLookup 194 //entityManagerFactoryProperties.put("hibernate.transaction.flush_before_completion", ""); // If enabled, the session will be automatically flushed during the before completion phase of the transaction. Built-in and automatic session context management is preferred, see Section 2.5, “Contextual sessions”. e.g. true | false 195 //entityManagerFactoryProperties.put("hibernate.transaction.auto_close_session", ""); // If enabled, the session will be automatically closed during the after completion phase of the transaction. Built-in and automatic session context management is preferred, see Section 2.5, “Contextual sessions”. e.g. true | false 196 197 // **** Miscellaneous Properties **** 198 //entityManagerFactoryProperties.put("hibernate.current_session_context_class", ""); // Supply a custom strategy for the scoping of the "current" Session. See Section 2.5, “Contextual sessions” for more information about the built-in strategies. e.g. jta | thread | managed | custom.Class 199 //entityManagerFactoryProperties.put("hibernate.query.factory_class", ""); // Chooses the HQL parser implementation. e.g. org.hibernate.hql.ast.ASTQueryTranslatorFactory or org.hibernate.hql.classic.ClassicQueryTranslatorFactory 200 //entityManagerFactoryProperties.put("hibernate.query.substitutions", ""); // Is used to map from tokens in Hibernate queries to SQL tokens (tokens might be function or literal names, for example). e.g. hqlLiteral=SQL_LITERAL, hqlFunction=SQLFUNC 201 //entityManagerFactoryProperties.put("hibernate.hbm2ddl.auto", "validate"); // Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. e.g. validate | update | create | create-drop 202 //entityManagerFactoryProperties.put("hibernate.bytecode.use_reflection_optimizer", "true"); // Enables the use of CGLIB instead of runtime reflection (System-level property). Reflection can sometimes be useful when troubleshooting. Hibernate always requires CGLIB even if you turn off the optimizer. You cannot set this property in hibernate.cfg.xml. e.g. true | false 203 204 // **** Custom Hibernate properties **** 205 if (configurationMap != null && !configurationMap.isEmpty()) { 206 for (final String configKey : configurationMap.keySet()) { 207 // TODO: QA on added custom properties... 208 entityManagerFactoryProperties.put(configKey, configurationMap.get(configKey)); 209 } 210 } 211 212 if (log.isDebugEnabled()) { 213 log.debug("Resolved entity manager factory properties: " + entityManagerFactoryProperties); 214 } 215 return entityManagerFactoryProperties; 216 } 217 }