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                      StringBuilder sb = new StringBuilder();
71  
72                      sb.append("Liferay is configured to use Hypersonic as ");
73                      sb.append("its database. Do NOT use Hypersonic in ");
74                      sb.append("production. Hypersonic is an embedded ");
75                      sb.append("database useful for development and demo'ing ");
76                      sb.append("purposes. The database settings can be ");
77                      sb.append("changed in portal.properties.");
78  
79                      _log.warn(sb.toString());
80                  }
81              }
82  
83              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
84                  dialect = new SybaseDialect();
85              }
86              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
87                  dialect = new DB2Dialect();
88              }
89              else {
90                  dialect = DialectFactory.determineDialect(
91                      dbName, dbMajorVersion);
92              }
93  
94              DBUtil.setInstance(dialect);
95  
96              if (_log.isInfoEnabled()) {
97                  _log.info("Using dialect " + dialect.getClass().getName());
98              }
99          }
100         catch (Exception e) {
101             String msg = GetterUtil.getString(e.getMessage());
102 
103             if (msg.indexOf("explicitly set for database: DB2") != -1) {
104                 dialect = new DB2400Dialect();
105 
106                 if (_log.isWarnEnabled()) {
107                     _log.warn(
108                         "DB2400Dialect was dynamically chosen as the " +
109                             "Hibernate dialect for DB2. This can be " +
110                                 "overriden in portal.properties");
111                 }
112             }
113             else {
114                 _log.error(e, e);
115             }
116         }
117         finally {
118             DataAccess.cleanUp(con);
119         }
120 
121         if (dialect == null) {
122             throw new RuntimeException("No dialect found");
123         }
124 
125         return dialect.getClass().getName();
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
129 
130 }