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