1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.transaction.xa.jta.connection.dialect;
19
20 import lombok.SneakyThrows;
21 import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper;
22
23 import javax.sql.XAConnection;
24 import javax.sql.XADataSource;
25 import java.lang.reflect.Constructor;
26 import java.sql.Connection;
27 import java.sql.SQLException;
28 import java.util.Properties;
29
30
31
32
33 public final class OracleXAConnectionWrapper implements XAConnectionWrapper {
34
35 private Class<Connection> jdbcConnectionClass;
36
37 private Constructor<?> xaConnectionConstructor;
38
39 @Override
40 public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) throws SQLException {
41 return createXAConnection(connection.unwrap(jdbcConnectionClass));
42 }
43
44 @Override
45 public void init(final Properties props) {
46 loadReflection();
47 }
48
49 private void loadReflection() {
50 jdbcConnectionClass = getJDBCConnectionClass();
51 xaConnectionConstructor = getXAConnectionConstructor();
52 }
53
54 @SuppressWarnings("unchecked")
55 @SneakyThrows(ReflectiveOperationException.class)
56 private Class<Connection> getJDBCConnectionClass() {
57 return (Class<Connection>) Class.forName("oracle.jdbc.internal.OracleConnection");
58 }
59
60 @SneakyThrows(ReflectiveOperationException.class)
61 private Constructor<?> getXAConnectionConstructor() {
62 return Class.forName("oracle.jdbc.xa.client.OracleXAConnection").getConstructor(Connection.class);
63 }
64
65 @SneakyThrows(ReflectiveOperationException.class)
66 private XAConnection createXAConnection(final Connection connection) {
67 return (XAConnection) xaConnectionConstructor.newInstance(connection);
68 }
69
70 @Override
71 public String getDatabaseType() {
72 return "Oracle";
73 }
74 }