1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  import com.liferay.portal.kernel.util.StringBundler;
24  
25  import java.sql.Connection;
26  import java.sql.DatabaseMetaData;
27  
28  import java.util.Properties;
29  
30  import javax.sql.DataSource;
31  
32  import org.hibernate.dialect.DB2400Dialect;
33  import org.hibernate.dialect.Dialect;
34  import org.hibernate.dialect.SybaseASE15Dialect;
35  import org.hibernate.dialect.resolver.DialectFactory;
36  
37  /**
38   * <a href="DialectDetector.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class DialectDetector {
43  
44      public static String determineDialect(DataSource dataSource) {
45          Dialect dialect = getDialect(dataSource);
46  
47          DBFactoryUtil.setDB(dialect);
48  
49          if (_log.isInfoEnabled()) {
50              _log.info("Using dialect " + dialect.getClass().getName());
51          }
52  
53          return dialect.getClass().getName();
54      }
55  
56      public static Dialect getDialect(DataSource dataSource) {
57          Dialect dialect = null;
58  
59          Connection connection = null;
60  
61          try {
62              connection = dataSource.getConnection();
63  
64              DatabaseMetaData databaseMetaData = connection.getMetaData();
65  
66              String dbName = databaseMetaData.getDatabaseProductName();
67              int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
68  
69              if (_log.isInfoEnabled()) {
70                  _log.info(
71                      "Determining dialect for " + dbName + " " + dbMajorVersion);
72              }
73  
74              if (dbName.startsWith("HSQL")) {
75                  if (_log.isWarnEnabled()) {
76                      StringBundler sb = new StringBundler(6);
77  
78                      sb.append("Liferay is configured to use Hypersonic as ");
79                      sb.append("its database. Do NOT use Hypersonic in ");
80                      sb.append("production. Hypersonic is an embedded ");
81                      sb.append("database useful for development and demo'ing ");
82                      sb.append("purposes. The database settings can be ");
83                      sb.append("changed in portal.properties.");
84  
85                      _log.warn(sb.toString());
86                  }
87              }
88  
89              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
90                  dialect = new SybaseASE15Dialect();
91              }
92              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
93                  dialect = new DB2Dialect();
94              }
95              else {
96                  dialect = DialectFactory.buildDialect(
97                      new Properties(), connection);
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(connection);
119         }
120 
121         if (dialect == null) {
122             throw new RuntimeException("No dialect found");
123         }
124 
125         return dialect;
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
129 
130 }