001    /**
002     * Copyright (c) 2000-2012 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.SQLServer2005Dialect;
019    import com.liferay.portal.dao.orm.hibernate.SQLServer2008Dialect;
020    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
021    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
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.SybaseASE15Dialect;
041    import org.hibernate.dialect.resolver.DialectFactory;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class DialectDetector {
047    
048            public static String determineDialect(DataSource dataSource) {
049                    Dialect dialect = getDialect(dataSource);
050    
051                    DBFactoryUtil.setDB(dialect);
052    
053                    return dialect.getClass().getName();
054            }
055    
056            public static Dialect getDialect(DataSource dataSource) {
057                    String dialectKey = null;
058                    Dialect dialect = null;
059    
060                    Connection connection = null;
061    
062                    try {
063                            connection = dataSource.getConnection();
064    
065                            DatabaseMetaData databaseMetaData = connection.getMetaData();
066    
067                            String dbName = databaseMetaData.getDatabaseProductName();
068                            int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
069    
070                            dialectKey = dbName.concat(StringPool.COLON).concat(
071                                    String.valueOf(dbMajorVersion));
072    
073                            dialect = _dialects.get(dialectKey);
074    
075                            if (dialect != null) {
076                                    return dialect;
077                            }
078    
079                            if (_log.isInfoEnabled()) {
080                                    _log.info(
081                                            "Determine dialect for " + dbName + " " + dbMajorVersion);
082                            }
083    
084                            if (dbName.startsWith("HSQL")) {
085                                    if (_log.isWarnEnabled()) {
086                                            StringBundler sb = new StringBundler(6);
087    
088                                            sb.append("Liferay is configured to use Hypersonic as ");
089                                            sb.append("its database. Do NOT use Hypersonic in ");
090                                            sb.append("production. Hypersonic is an embedded ");
091                                            sb.append("database useful for development and demo'ing ");
092                                            sb.append("purposes. The database settings can be ");
093                                            sb.append("changed in portal-ext.properties.");
094    
095                                            _log.warn(sb.toString());
096                                    }
097                            }
098    
099                            if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
100                                    dialect = new SybaseASE15Dialect();
101                            }
102                            else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
103                                    dialect = new DB2Dialect();
104                            }
105                            else if (dbName.startsWith("Microsoft") && (dbMajorVersion == 9)) {
106                                    dialect = new SQLServer2005Dialect();
107                            }
108                            else if (dbName.startsWith("Microsoft") && (dbMajorVersion == 10)) {
109                                    dialect = new SQLServer2008Dialect();
110                            }
111                            else if (dbName.startsWith("Oracle") && (dbMajorVersion >= 10)) {
112                                    dialect = new Oracle10gDialect();
113                            }
114                            else {
115                                    dialect = DialectFactory.buildDialect(
116                                            new Properties(), connection);
117                            }
118                    }
119                    catch (Exception e) {
120                            String msg = GetterUtil.getString(e.getMessage());
121    
122                            if (msg.indexOf("explicitly set for database: DB2") != -1) {
123                                    dialect = new DB2400Dialect();
124    
125                                    if (_log.isWarnEnabled()) {
126                                            _log.warn(
127                                                    "DB2400Dialect was dynamically chosen as the " +
128                                                            "Hibernate dialect for DB2. This can be " +
129                                                                    "overriden in portal.properties");
130                                    }
131                            }
132                            else {
133                                    _log.error(e, e);
134                            }
135                    }
136                    finally {
137                            DataAccess.cleanUp(connection);
138                    }
139    
140                    if (dialect == null) {
141                            throw new RuntimeException("No dialect found");
142                    }
143                    else if (dialectKey != null) {
144                            if (_log.isInfoEnabled()) {
145                                    _log.info("Found dialect " + dialect.getClass().getName());
146                            }
147    
148                            _dialects.put(dialectKey, dialect);
149                    }
150    
151                    return dialect;
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
155    
156            private static Map<String, Dialect> _dialects =
157                    new ConcurrentHashMap<String, Dialect>();
158    
159    }