001    /**
002     * Copyright (c) 2000-2010 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.SQLServer2008Dialect;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    
026    import java.sql.Connection;
027    import java.sql.DatabaseMetaData;
028    
029    import java.util.Properties;
030    
031    import javax.sql.DataSource;
032    
033    import org.hibernate.dialect.DB2400Dialect;
034    import org.hibernate.dialect.Dialect;
035    import org.hibernate.dialect.SybaseASE15Dialect;
036    import org.hibernate.dialect.resolver.DialectFactory;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class DialectDetector {
042    
043            public static String determineDialect(DataSource dataSource) {
044                    Dialect dialect = getDialect(dataSource);
045    
046                    DBFactoryUtil.setDB(dialect);
047    
048                    if (_log.isInfoEnabled()) {
049                            _log.info("Using dialect " + dialect.getClass().getName());
050                    }
051    
052                    return dialect.getClass().getName();
053            }
054    
055            public static Dialect getDialect(DataSource dataSource) {
056                    Dialect dialect = null;
057    
058                    Connection connection = null;
059    
060                    try {
061                            connection = dataSource.getConnection();
062    
063                            DatabaseMetaData databaseMetaData = connection.getMetaData();
064    
065                            String dbName = databaseMetaData.getDatabaseProductName();
066                            int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
067    
068                            if (_log.isInfoEnabled()) {
069                                    _log.info(
070                                            "Determining dialect for " + dbName + " " + dbMajorVersion);
071                            }
072    
073                            if (dbName.startsWith("HSQL")) {
074                                    if (_log.isWarnEnabled()) {
075                                            StringBundler sb = new StringBundler(6);
076    
077                                            sb.append("Liferay is configured to use Hypersonic as ");
078                                            sb.append("its database. Do NOT use Hypersonic in ");
079                                            sb.append("production. Hypersonic is an embedded ");
080                                            sb.append("database useful for development and demo'ing ");
081                                            sb.append("purposes. The database settings can be ");
082                                            sb.append("changed in portal.properties.");
083    
084                                            _log.warn(sb.toString());
085                                    }
086                            }
087    
088                            if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
089                                    dialect = new SybaseASE15Dialect();
090                            }
091                            else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
092                                    dialect = new DB2Dialect();
093                            }
094                            else if (dbName.startsWith("Microsoft") && (dbMajorVersion >= 9)) {
095                                    dialect = new SQLServer2008Dialect();
096                            }
097                            else {
098                                    dialect = DialectFactory.buildDialect(
099                                            new Properties(), connection);
100                            }
101                    }
102                    catch (Exception e) {
103                            String msg = GetterUtil.getString(e.getMessage());
104    
105                            if (msg.indexOf("explicitly set for database: DB2") != -1) {
106                                    dialect = new DB2400Dialect();
107    
108                                    if (_log.isWarnEnabled()) {
109                                            _log.warn(
110                                                    "DB2400Dialect was dynamically chosen as the " +
111                                                            "Hibernate dialect for DB2. This can be " +
112                                                                    "overriden in portal.properties");
113                                    }
114                            }
115                            else {
116                                    _log.error(e, e);
117                            }
118                    }
119                    finally {
120                            DataAccess.cleanUp(connection);
121                    }
122    
123                    if (dialect == null) {
124                            throw new RuntimeException("No dialect found");
125                    }
126    
127                    return dialect;
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
131    
132    }