1
14
15 package com.liferay.portal.spring.hibernate;
16
17 import com.liferay.portal.dao.orm.hibernate.DB2Dialect;
18 import com.liferay.portal.dao.orm.hibernate.SQLServer2008Dialect;
19 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
20 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.StringBundler;
25
26 import java.sql.Connection;
27 import java.sql.DatabaseMetaData;
28
29 import java.util.Properties;
30
31 import javax.sql.DataSource;
32
33 import org.hibernate.dialect.DB2400Dialect;
34 import org.hibernate.dialect.Dialect;
35 import org.hibernate.dialect.SybaseASE15Dialect;
36 import org.hibernate.dialect.resolver.DialectFactory;
37
38
43 public class DialectDetector {
44
45 public static String determineDialect(DataSource dataSource) {
46 Dialect dialect = getDialect(dataSource);
47
48 DBFactoryUtil.setDB(dialect);
49
50 if (_log.isInfoEnabled()) {
51 _log.info("Using dialect " + dialect.getClass().getName());
52 }
53
54 return dialect.getClass().getName();
55 }
56
57 public static Dialect getDialect(DataSource dataSource) {
58 Dialect dialect = null;
59
60 Connection connection = null;
61
62 try {
63 connection = dataSource.getConnection();
64
65 DatabaseMetaData databaseMetaData = connection.getMetaData();
66
67 String dbName = databaseMetaData.getDatabaseProductName();
68 int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
69
70 if (_log.isInfoEnabled()) {
71 _log.info(
72 "Determining dialect for " + dbName + " " + dbMajorVersion);
73 }
74
75 if (dbName.startsWith("HSQL")) {
76 if (_log.isWarnEnabled()) {
77 StringBundler sb = new StringBundler(6);
78
79 sb.append("Liferay is configured to use Hypersonic as ");
80 sb.append("its database. Do NOT use Hypersonic in ");
81 sb.append("production. Hypersonic is an embedded ");
82 sb.append("database useful for development and demo'ing ");
83 sb.append("purposes. The database settings can be ");
84 sb.append("changed in portal.properties.");
85
86 _log.warn(sb.toString());
87 }
88 }
89
90 if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
91 dialect = new SybaseASE15Dialect();
92 }
93 else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
94 dialect = new DB2Dialect();
95 }
96 else if (dbName.startsWith("Microsoft") && (dbMajorVersion >= 9)) {
97 dialect = new SQLServer2008Dialect();
98 }
99 else {
100 dialect = DialectFactory.buildDialect(
101 new Properties(), connection);
102 }
103 }
104 catch (Exception e) {
105 String msg = GetterUtil.getString(e.getMessage());
106
107 if (msg.indexOf("explicitly set for database: DB2") != -1) {
108 dialect = new DB2400Dialect();
109
110 if (_log.isWarnEnabled()) {
111 _log.warn(
112 "DB2400Dialect was dynamically chosen as the " +
113 "Hibernate dialect for DB2. This can be " +
114 "overriden in portal.properties");
115 }
116 }
117 else {
118 _log.error(e, e);
119 }
120 }
121 finally {
122 DataAccess.cleanUp(connection);
123 }
124
125 if (dialect == null) {
126 throw new RuntimeException("No dialect found");
127 }
128
129 return dialect;
130 }
131
132 private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
133
134 }