1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.hibernate;
24  
25  import com.liferay.portal.dao.orm.hibernate.DB2Dialect;
26  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
27  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  
32  import java.sql.Connection;
33  import java.sql.DatabaseMetaData;
34  
35  import javax.sql.DataSource;
36  
37  import org.hibernate.dialect.DB2400Dialect;
38  import org.hibernate.dialect.Dialect;
39  import org.hibernate.dialect.DialectFactory;
40  import org.hibernate.dialect.SybaseDialect;
41  
42  /**
43   * <a href="DialectDetector.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class DialectDetector {
48  
49      public static String determineDialect(DataSource dataSource) {
50          Dialect dialect = getDialect(dataSource);
51  
52          DBFactoryUtil.setDB(dialect);
53  
54          if (_log.isInfoEnabled()) {
55              _log.info("Using dialect " + dialect.getClass().getName());
56          }
57  
58          return dialect.getClass().getName();
59      }
60  
61      public static Dialect getDialect(DataSource dataSource) {
62          Dialect dialect = null;
63  
64          Connection connection = null;
65  
66          try {
67              connection = dataSource.getConnection();
68  
69              DatabaseMetaData databaseMetaData = connection.getMetaData();
70  
71              String dbName = databaseMetaData.getDatabaseProductName();
72              int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
73  
74              if (_log.isInfoEnabled()) {
75                  _log.info(
76                      "Determining dialect for " + dbName + " " + dbMajorVersion);
77              }
78  
79              if (dbName.startsWith("HSQL")) {
80                  if (_log.isWarnEnabled()) {
81                      StringBuilder sb = new StringBuilder();
82  
83                      sb.append("Liferay is configured to use Hypersonic as ");
84                      sb.append("its database. Do NOT use Hypersonic in ");
85                      sb.append("production. Hypersonic is an embedded ");
86                      sb.append("database useful for development and demo'ing ");
87                      sb.append("purposes. The database settings can be ");
88                      sb.append("changed in portal.properties.");
89  
90                      _log.warn(sb.toString());
91                  }
92              }
93  
94              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
95                  dialect = new SybaseDialect();
96              }
97              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
98                  dialect = new DB2Dialect();
99              }
100             else {
101                 dialect = DialectFactory.determineDialect(
102                     dbName, dbMajorVersion);
103             }
104         }
105         catch (Exception e) {
106             String msg = GetterUtil.getString(e.getMessage());
107 
108             if (msg.indexOf("explicitly set for database: DB2") != -1) {
109                 dialect = new DB2400Dialect();
110 
111                 if (_log.isWarnEnabled()) {
112                     _log.warn(
113                         "DB2400Dialect was dynamically chosen as the " +
114                             "Hibernate dialect for DB2. This can be " +
115                                 "overriden in portal.properties");
116                 }
117             }
118             else {
119                 _log.error(e, e);
120             }
121         }
122         finally {
123             DataAccess.cleanUp(connection);
124         }
125 
126         if (dialect == null) {
127             throw new RuntimeException("No dialect found");
128         }
129 
130         return dialect;
131     }
132 
133     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
134 
135 }