1
14
15 package com.liferay.portal.spring.jpa;
16
17 import com.liferay.portal.kernel.dao.db.DB;
18 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.GetterUtil;
23 import com.liferay.portal.kernel.util.StringBundler;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.util.PropsValues;
26
27 import java.sql.Connection;
28 import java.sql.DatabaseMetaData;
29
30 import javax.sql.DataSource;
31
32 import org.springframework.orm.jpa.vendor.Database;
33
34
40 public class DatabaseDetector {
41
42 public static Database determineDatabase(DataSource dataSource) {
43 Database database = null;
44 String type = null;
45
46 Connection connection = null;
47
48 try {
49 connection = dataSource.getConnection();
50
51 DatabaseMetaData databaseMetaData = connection.getMetaData();
52
53 String dbName = databaseMetaData.getDatabaseProductName();
54 int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
55
56 if (_log.isInfoEnabled()) {
57 _log.info(
58 "Determining DB type for " + dbName + " " + dbMajorVersion);
59 }
60
61 if (dbName.equals("Apache Derby")) {
62 database = Database.DERBY;
63 type = DB.TYPE_DERBY;
64 }
65 else if (dbName.startsWith("DB2/")) {
66 database = Database.DB2;
67 type = DB.TYPE_DB2;
68 }
69 else if (dbName.equals("HSQL Database Engine")) {
70 if (_log.isWarnEnabled()) {
71 StringBundler sb = new StringBundler(6);
72
73 sb.append("Liferay is configured to use Hypersonic as ");
74 sb.append("its database. Do NOT use Hypersonic in ");
75 sb.append("production. Hypersonic is an embedded ");
76 sb.append("database useful for development and demo'ing ");
77 sb.append("purposes. The database settings can be ");
78 sb.append("changed in portal.properties.");
79
80 _log.warn(sb.toString());
81 }
82
83 database = Database.HSQL;
84 type = DB.TYPE_HYPERSONIC;
85 }
86 else if (dbName.equals("Informix Dynamic Server")) {
87 database = Database.INFORMIX;
88 type = DB.TYPE_INFORMIX;
89 }
90 else if (dbName.startsWith("Microsoft SQL Server")) {
91 database = Database.SQL_SERVER;
92 type = DB.TYPE_SQLSERVER;
93 }
94 else if (dbName.equals("MySQL")) {
95 database = Database.MYSQL;
96 type = DB.TYPE_MYSQL;
97 }
98 else if (dbName.equals("Oracle")) {
99 database = Database.ORACLE;
100 type = DB.TYPE_ORACLE;
101 }
102 else if (dbName.equals("PostgreSQL")) {
103 database = Database.POSTGRESQL;
104 type = DB.TYPE_POSTGRESQL;
105 }
106 else if (dbName.equals("Sybase SQL Server")) {
107 database = Database.SYBASE;
108 type = DB.TYPE_SYBASE;
109 }
110
111 if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
112 database = Database.SYBASE;
113 type = DB.TYPE_SYBASE;
114 }
115 }
116 catch (Exception e) {
117 String msg = GetterUtil.getString(e.getMessage());
118
119 if (msg.indexOf("explicitly set for database: DB2") != -1) {
120 database = Database.DB2;
121
122 type = DB.TYPE_DB2;
123 }
124 else {
125 _log.error(e, e);
126 }
127 }
128 finally {
129 DataAccess.cleanUp(connection);
130 }
131
132 if (database == null) {
133 throw new RuntimeException("Unable to detect the database");
134 }
135
136 if (_log.isInfoEnabled()) {
137 _log.info("Detected database " + database.toString());
138 }
139
140 if (Validator.isNotNull(PropsValues.JPA_DATABASE_TYPE)) {
141 DBFactoryUtil.setDB(PropsValues.JPA_DATABASE_TYPE);
142 }
143 else {
144 DBFactoryUtil.setDB(type);
145 }
146
147 return database;
148 }
149
150 private static Log _log = LogFactoryUtil.getLog(DatabaseDetector.class);
151
152 }