1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.hibernate;
21  
22  import com.liferay.portal.dao.orm.hibernate.DB2Dialect;
23  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.tools.sql.DBUtil;
28  
29  import java.sql.Connection;
30  import java.sql.DatabaseMetaData;
31  
32  import javax.sql.DataSource;
33  
34  import org.hibernate.dialect.DB2400Dialect;
35  import org.hibernate.dialect.Dialect;
36  import org.hibernate.dialect.DialectFactory;
37  import org.hibernate.dialect.SybaseDialect;
38  
39  /**
40   * <a href="DialectDetector.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class DialectDetector {
46  
47      public static String determineDialect(DataSource dataSource) {
48          Dialect dialect = null;
49  
50          Connection con = null;
51  
52          try {
53              con = dataSource.getConnection();
54  
55              DatabaseMetaData metaData = con.getMetaData();
56  
57              String dbName = metaData.getDatabaseProductName();
58              int dbMajorVersion = metaData.getDatabaseMajorVersion();
59  
60              if (_log.isInfoEnabled()) {
61                  _log.info(
62                      "Determining dialect for " + dbName + " " + dbMajorVersion);
63              }
64  
65              if (dbName.startsWith("HSQL")) {
66                  if (_log.isWarnEnabled()) {
67                      _log.warn(
68                          "Liferay is configured to use Hypersonic as its " +
69                              "database. Do NOT use Hypersonic in production. " +
70                                  "Hypersonic is an embedded database useful " +
71                                      "for development and demo'ing purposes.");
72                  }
73              }
74  
75              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
76                  dialect = new SybaseDialect();
77              }
78              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
79                  dialect = new DB2Dialect();
80              }
81              else {
82                  dialect = DialectFactory.determineDialect(
83                      dbName, dbMajorVersion);
84              }
85  
86              DBUtil.setInstance(dialect);
87  
88              if (_log.isInfoEnabled()) {
89                  _log.info("Using dialect " + dialect.getClass().getName());
90              }
91          }
92          catch (Exception e) {
93              String msg = GetterUtil.getString(e.getMessage());
94  
95              if (msg.indexOf("explicitly set for database: DB2") != -1) {
96                  dialect = new DB2400Dialect();
97  
98                  if (_log.isWarnEnabled()) {
99                      _log.warn(
100                         "DB2400Dialect was dynamically chosen as the " +
101                             "Hibernate dialect for DB2. This can be " +
102                                 "overriden in portal.properties");
103                 }
104             }
105             else {
106                 _log.error(e, e);
107             }
108         }
109         finally {
110             DataAccess.cleanUp(con);
111         }
112 
113         if (dialect == null) {
114             throw new RuntimeException("No dialect found");
115         }
116 
117         return dialect.getClass().getName();
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
121 
122 }