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
48 public class DialectDetector {
49
50 public static String determineDialect(DataSource dataSource) {
51 Dialect dialect = null;
52
53 Connection con = null;
54
55 try {
56 con = dataSource.getConnection();
57
58 DatabaseMetaData metaData = con.getMetaData();
59
60 String dbName = metaData.getDatabaseProductName();
61 int dbMajorVersion = metaData.getDatabaseMajorVersion();
62
63 if (_log.isInfoEnabled()) {
64 _log.info(
65 "Determining dialect for " + dbName + " " + dbMajorVersion);
66 }
67
68 if (dbName.startsWith("HSQL")) {
69 if (_log.isWarnEnabled()) {
70 StringBuilder sb = new StringBuilder();
71
72 sb.append("Liferay is configured to use Hypersonic as ");
73 sb.append("its database. Do NOT use Hypersonic in ");
74 sb.append("production. Hypersonic is an embedded ");
75 sb.append("database useful for development and demo'ing ");
76 sb.append("purposes. The database settings can be ");
77 sb.append("changed in portal.properties.");
78
79 _log.warn(sb.toString());
80 }
81 }
82
83 if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
84 dialect = new SybaseDialect();
85 }
86 else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
87 dialect = new DB2Dialect();
88 }
89 else {
90 dialect = DialectFactory.determineDialect(
91 dbName, dbMajorVersion);
92 }
93
94 DBUtil.setInstance(dialect);
95
96 if (_log.isInfoEnabled()) {
97 _log.info("Using dialect " + dialect.getClass().getName());
98 }
99 }
100 catch (Exception e) {
101 String msg = GetterUtil.getString(e.getMessage());
102
103 if (msg.indexOf("explicitly set for database: DB2") != -1) {
104 dialect = new DB2400Dialect();
105
106 if (_log.isWarnEnabled()) {
107 _log.warn(
108 "DB2400Dialect was dynamically chosen as the " +
109 "Hibernate dialect for DB2. This can be " +
110 "overriden in portal.properties");
111 }
112 }
113 else {
114 _log.error(e, e);
115 }
116 }
117 finally {
118 DataAccess.cleanUp(con);
119 }
120
121 if (dialect == null) {
122 throw new RuntimeException("No dialect found");
123 }
124
125 return dialect.getClass().getName();
126 }
127
128 private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
129
130 }