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 _log.warn(
71 "Liferay is configured to use Hypersonic as its " +
72 "database. Do NOT use Hypersonic in production. " +
73 "Hypersonic is an embedded database useful " +
74 "for development and demo'ing purposes.");
75 }
76 }
77
78 if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
79 dialect = new SybaseDialect();
80 }
81 else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
82 dialect = new DB2Dialect();
83 }
84 else {
85 dialect = DialectFactory.determineDialect(
86 dbName, dbMajorVersion);
87 }
88
89 DBUtil.setInstance(dialect);
90
91 if (_log.isInfoEnabled()) {
92 _log.info("Using dialect " + dialect.getClass().getName());
93 }
94 }
95 catch (Exception e) {
96 String msg = GetterUtil.getString(e.getMessage());
97
98 if (msg.indexOf("explicitly set for database: DB2") != -1) {
99 dialect = new DB2400Dialect();
100
101 if (_log.isWarnEnabled()) {
102 _log.warn(
103 "DB2400Dialect was dynamically chosen as the " +
104 "Hibernate dialect for DB2. This can be " +
105 "overriden in portal.properties");
106 }
107 }
108 else {
109 _log.error(e, e);
110 }
111 }
112 finally {
113 DataAccess.cleanUp(con);
114 }
115
116 if (dialect == null) {
117 throw new RuntimeException("No dialect found");
118 }
119
120 return dialect.getClass().getName();
121 }
122
123 private static Log _log = LogFactoryUtil.getLog(DialectDetector.class);
124
125 }