001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.hibernate;
016    
017    import com.liferay.portal.dao.orm.hibernate.DB2Dialect;
018    import com.liferay.portal.dao.orm.hibernate.HSQLDialect;
019    import com.liferay.portal.dao.orm.hibernate.SQLServer2005Dialect;
020    import com.liferay.portal.dao.orm.hibernate.SQLServer2008Dialect;
021    import com.liferay.portal.dao.orm.hibernate.SybaseASE157Dialect;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    
028    import java.sql.Connection;
029    import java.sql.DatabaseMetaData;
030    
031    import java.util.Map;
032    import java.util.Properties;
033    import java.util.concurrent.ConcurrentHashMap;
034    
035    import javax.sql.DataSource;
036    
037    import org.hibernate.dialect.DB2400Dialect;
038    import org.hibernate.dialect.Dialect;
039    import org.hibernate.dialect.Oracle10gDialect;
040    import org.hibernate.dialect.resolver.DialectFactory;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class DialectDetector {
046    
047            public static Dialect getDialect(DataSource dataSource) {
048                    String dialectKey = null;
049                    Dialect dialect = null;
050    
051                    try (Connection connection = dataSource.getConnection()) {
052                            DatabaseMetaData databaseMetaData = connection.getMetaData();
053    
054                            String dbName = databaseMetaData.getDatabaseProductName();
055                            int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
056                            int dbMinorVersion = databaseMetaData.getDatabaseMinorVersion();
057    
058                            StringBundler sb = new StringBundler(5);
059    
060                            sb.append(dbName);
061                            sb.append(StringPool.COLON);
062                            sb.append(dbMajorVersion);
063                            sb.append(StringPool.COLON);
064                            sb.append(dbMinorVersion);
065    
066                            dialectKey = sb.toString();
067    
068                            dialect = _dialects.get(dialectKey);
069    
070                            if (dialect != null) {
071                                    return dialect;
072                            }
073    
074                            if (_log.isInfoEnabled()) {
075                                    _log.info(
076                                            "Determine dialect for " + dbName + " " + dbMajorVersion +
077                                                    "." + dbMinorVersion);
078                            }
079    
080                            if (dbName.startsWith("HSQL")) {
081                                    dialect = new HSQLDialect();
082    
083                                    if (_log.isWarnEnabled()) {
084                                            sb = new StringBundler(6);
085    
086                                            sb.append("Liferay is configured to use Hypersonic as ");
087                                            sb.append("its database. Do NOT use Hypersonic in ");
088                                            sb.append("production. Hypersonic is an embedded ");
089                                            sb.append("database useful for development and demo'ing ");
090                                            sb.append("purposes. The database settings can be ");
091                                            sb.append("changed in portal-ext.properties.");
092    
093                                            _log.warn(sb.toString());
094                                    }
095                            }
096                            else if (dbName.equals("Adaptive Server Enterprise") &&
097                                             (dbMajorVersion >= 15)) {
098    
099                                    dialect = new SybaseASE157Dialect();
100                            }
101                            else if (dbName.equals("ASE")) {
102                                    throw new RuntimeException(
103                                            "jTDS is no longer suppported. Please use the Sybase " +
104                                                    "JDBC driver to connect to Sybase.");
105                            }
106                            else if (dbName.startsWith("DB2") && (dbMajorVersion >= 9)) {
107                                    dialect = new DB2Dialect();
108                            }
109                            else if (dbName.startsWith("Microsoft") && (dbMajorVersion == 9)) {
110                                    dialect = new SQLServer2005Dialect();
111                            }
112                            else if (dbName.startsWith("Microsoft") && (dbMajorVersion == 10)) {
113                                    dialect = new SQLServer2008Dialect();
114                            }
115                            else if (dbName.startsWith("Oracle") && (dbMajorVersion >= 10)) {
116                                    dialect = new Oracle10gDialect();
117                            }
118                            else {
119                                    dialect = DialectFactory.buildDialect(
120                                            new Properties(), connection);
121                            }
122                    }
123                    catch (Exception e) {
124                            String msg = GetterUtil.getString(e.getMessage());
125    
126                            if (msg.contains("explicitly set for database: DB2")) {
127                                    dialect = new DB2400Dialect();
128    
129                                    if (_log.isWarnEnabled()) {
130                                            _log.warn(
131                                                    "DB2400Dialect was dynamically chosen as the " +
132                                                            "Hibernate dialect for DB2. This can be " +
133                                                                    "overriden in portal.properties");
134                                    }
135                            }
136                            else {
137                                    _log.error(e, e);
138                            }
139                    }
140    
141                    if (dialect == null) {
142                            throw new RuntimeException("No dialect found");
143                    }
144                    else if (dialectKey != null) {
145                            if (_log.isInfoEnabled()) {
146                                    _log.info("Found dialect " + dialect.getClass().getName());
147                            }
148    
149                            _dialects.put(dialectKey, dialect);
150                    }
151    
152                    return dialect;
153            }
154    
155            private static final Log _log = LogFactoryUtil.getLog(
156                    DialectDetector.class);
157    
158            private static final Map<String, Dialect> _dialects =
159                    new ConcurrentHashMap<>();
160    
161    }