1   /**
2    * Copyright (c) 2000-2008 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.jdbc.DataAccess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.tools.sql.DBUtil;
29  
30  import java.sql.Connection;
31  import java.sql.DatabaseMetaData;
32  
33  import javax.sql.DataSource;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import org.hibernate.dialect.DB2400Dialect;
39  import org.hibernate.dialect.Dialect;
40  import org.hibernate.dialect.DialectFactory;
41  import org.hibernate.dialect.SybaseDialect;
42  
43  /**
44   * <a href="DialectDetector.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class DialectDetector {
50  
51      public static String determineDialect(DataSource dataSource) {
52          Dialect dialect = null;
53  
54          Connection con = null;
55  
56          try {
57              con = dataSource.getConnection();
58  
59              DatabaseMetaData metaData = con.getMetaData();
60  
61              String dbName = metaData.getDatabaseProductName();
62              int dbMajorVersion = metaData.getDatabaseMajorVersion();
63  
64              if (_log.isInfoEnabled()) {
65                  _log.info(
66                      "Determining dialect for " + dbName + " " + dbMajorVersion);
67              }
68  
69              if (dbName.startsWith("HSQL")) {
70                  if (_log.isWarnEnabled()) {
71                      _log.warn(
72                          "Liferay is configured to use Hypersonic as its " +
73                              "database. Do NOT use Hypersonic in production. " +
74                                  "Hypersonic is an embedded database useful " +
75                                      "for development and demo'ing purposes.");
76                  }
77              }
78  
79              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
80                  dialect = new SybaseDialect();
81              }
82              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
83                  dialect = new DB2Dialect();
84              }
85              else {
86                  dialect = DialectFactory.determineDialect(
87                      dbName, dbMajorVersion);
88              }
89  
90              DBUtil.setInstance(dialect);
91  
92              if (_log.isInfoEnabled()) {
93                  _log.info("Using dialect " + dialect.getClass().getName());
94              }
95          }
96          catch (Exception e) {
97              String msg = GetterUtil.getString(e.getMessage());
98  
99              if (msg.indexOf("explicitly set for database: DB2") != -1) {
100                 dialect = new DB2400Dialect();
101 
102                 if (_log.isWarnEnabled()) {
103                     _log.warn(
104                         "DB2400Dialect was dynamically chosen as the " +
105                             "Hibernate dialect for DB2. This can be " +
106                                 "overriden in portal.properties");
107                 }
108             }
109             else {
110                 _log.error(e, e);
111             }
112         }
113         finally {
114             DataAccess.cleanUp(con);
115         }
116 
117         if (dialect == null) {
118             throw new RuntimeException("No dialect found");
119         }
120 
121         return dialect.getClass().getName();
122     }
123 
124     private static Log _log = LogFactory.getLog(DialectDetector.class);
125 
126 }