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