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