1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.platform;
17
18 import org.springframework.dao.DataAccessException;
19 import org.springframework.jdbc.core.ConnectionCallback;
20 import org.springframework.jdbc.core.JdbcTemplate;
21
22 import javax.sql.DataSource;
23 import java.sql.Connection;
24 import java.sql.DatabaseMetaData;
25 import java.sql.SQLException;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
28
29
30
31
32
33
34
35
36
37
38
39 public final class DatabasePlatforms {
40
41
42
43
44 public static final String ORACLE = "Oracle";
45
46
47
48
49 public static final String MYSQL = "MySQL";
50
51 private static final ConcurrentMap<DataSource, DatabasePlatformInfo> platformCache =
52 new ConcurrentHashMap<DataSource, DatabasePlatformInfo>();
53
54
55
56
57
58
59
60 public static DatabasePlatformInfo detectPlatform(DataSource dataSource) {
61 if (dataSource == null) {
62 throw new IllegalArgumentException("DataSource must not be null.");
63 }
64 DatabasePlatformInfo platformInfo = platformCache.get(dataSource);
65 if (platformInfo == null) {
66 JdbcTemplate template = new JdbcTemplate(dataSource);
67 platformInfo = platformCache.putIfAbsent(dataSource, template.execute(
68 new ConnectionCallback<DatabasePlatformInfo>() {
69 @Override
70 public DatabasePlatformInfo doInConnection(
71 Connection connection) throws SQLException, DataAccessException {
72 DatabaseMetaData metadata = connection.getMetaData();
73 String vendorName = metadata.getDatabaseProductName();
74 int version = metadata.getDatabaseMajorVersion();
75 return new DatabasePlatformInfo(vendorName, version);
76 }
77 }));
78 if (platformInfo == null) {
79 platformInfo = platformCache.get(dataSource);
80 }
81 }
82 return platformInfo;
83 }
84
85
86
87
88 private DatabasePlatforms() {}
89
90 }