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