1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.spring.hibernate;
16  
17  import com.liferay.portal.dao.orm.hibernate.DB2Dialect;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  
24  import java.sql.Connection;
25  import java.sql.DatabaseMetaData;
26  
27  import java.util.Properties;
28  
29  import javax.sql.DataSource;
30  
31  import org.hibernate.dialect.DB2400Dialect;
32  import org.hibernate.dialect.Dialect;
33  import org.hibernate.dialect.SybaseASE15Dialect;
34  import org.hibernate.dialect.resolver.DialectFactory;
35  
36  /**
37   * <a href="DialectDetector.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class DialectDetector {
42  
43      public static String determineDialect(DataSource dataSource) {
44          Dialect dialect = getDialect(dataSource);
45  
46          DBFactoryUtil.setDB(dialect);
47  
48          if (_log.isInfoEnabled()) {
49              _log.info("Using dialect " + dialect.getClass().getName());
50          }
51  
52          return dialect.getClass().getName();
53      }
54  
55      public static Dialect getDialect(DataSource dataSource) {
56          Dialect dialect = null;
57  
58          Connection connection = null;
59  
60          try {
61              connection = dataSource.getConnection();
62  
63              DatabaseMetaData databaseMetaData = connection.getMetaData();
64  
65              String dbName = databaseMetaData.getDatabaseProductName();
66              int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
67  
68              if (_log.isInfoEnabled()) {
69                  _log.info(
70                      "Determining dialect for " + dbName + " " + dbMajorVersion);
71              }
72  
73              if (dbName.startsWith("HSQL")) {
74                  if (_log.isWarnEnabled()) {
75                      _log.warn(
76                          "Liferay is configured to use Hypersonic as its " +
77                              "database. Do NOT use Hypersonic in production. " +
78                                  "Hypersonic is an embedded database useful " +
79                                      "for development and demo'ing purposes.");
80                  }
81              }
82  
83              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
84                  dialect = new SybaseASE15Dialect();
85              }
86              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
87                  dialect = new DB2Dialect();
88              }
89              else {
90                  dialect = DialectFactory.buildDialect(
91                      new Properties(), connection);
92              }
93          }
94          catch (Exception e) {
95              String msg = GetterUtil.getString(e.getMessage());
96  
97              if (msg.indexOf("explicitly set for database: DB2") != -1) {
98                  dialect = new DB2400Dialect();
99  
100                 if (_log.isWarnEnabled()) {
101                     _log.warn(
102                         "DB2400Dialect was dynamically chosen as the " +
103                             "Hibernate dialect for DB2. This can be " +
104                                 "overriden in portal.properties");
105                 }
106             }
107             else {
108                 _log.error(e, e);
109             }
110         }
111         finally {
112             DataAccess.cleanUp(connection);
113         }
114 
115         if (dialect == null) {
116             throw new RuntimeException("No dialect found");
117         }
118 
119         return dialect;
120     }
121 
122     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
123 
124 }