1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DialectDetector {
48  
49      public static String determineDialect(DataSource dataSource) {
50          Dialect dialect = null;
51  
52          Connection connection = null;
53  
54          try {
55              connection = dataSource.getConnection();
56  
57              DatabaseMetaData databaseMetaData = connection.getMetaData();
58  
59              String dbName = databaseMetaData.getDatabaseProductName();
60              int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
61  
62              if (_log.isInfoEnabled()) {
63                  _log.info(
64                      "Determining dialect for " + dbName + " " + dbMajorVersion);
65              }
66  
67              if (dbName.startsWith("HSQL")) {
68                  if (_log.isWarnEnabled()) {
69                      _log.warn(
70                          "Liferay is configured to use Hypersonic as its " +
71                              "database. Do NOT use Hypersonic in production. " +
72                                  "Hypersonic is an embedded database useful " +
73                                      "for development and demo'ing purposes.");
74                  }
75              }
76  
77              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
78                  dialect = new SybaseDialect();
79              }
80              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
81                  dialect = new DB2Dialect();
82              }
83              else {
84                  dialect = DialectFactory.determineDialect(
85                      dbName, dbMajorVersion);
86              }
87  
88              DBUtil.setInstance(dialect);
89  
90              if (_log.isInfoEnabled()) {
91                  _log.info("Using dialect " + dialect.getClass().getName());
92              }
93          }
94          catch (Exception e) {
95              String msg = GetterUtil.getString(e.getMessage());
96  
97              if (msg.indexOf("explicitly set for database: DB2") != -1) {
98                  dialect = new DB2400Dialect();
99  
100                 if (_log.isWarnEnabled()) {
101                     _log.warn(
102                         "DB2400Dialect was dynamically chosen as the " +
103                             "Hibernate dialect for DB2. This can be " +
104                                 "overriden in portal.properties");
105                 }
106             }
107             else {
108                 _log.error(e, e);
109             }
110         }
111         finally {
112             DataAccess.cleanUp(connection);
113         }
114 
115         if (dialect == null) {
116             throw new RuntimeException("No dialect found");
117         }
118 
119         return dialect.getClass().getName();
120     }
121 
122     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
123 
124 }