1 package org.synchronoss.cpo.jdbc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import javax.sql.DataSource;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.synchronoss.cpo.core.CpoException;
31
32
33
34
35
36
37
38
39
40 class JdbcPooledConnectionStrategy implements JdbcConnectionStrategy {
41
42
43 private static final long serialVersionUID = 1L;
44
45 private static final Logger logger = LoggerFactory.getLogger(JdbcPooledConnectionStrategy.class);
46
47 private final transient DataSource readDataSource;
48 private final transient DataSource writeDataSource;
49 private boolean invalidReadDataSource = false;
50
51
52
53
54
55
56
57 JdbcPooledConnectionStrategy(DataSource readDataSource, DataSource writeDataSource) {
58 this.readDataSource = readDataSource;
59 this.writeDataSource = writeDataSource;
60 }
61
62 @Override
63 public Connection getReadConnection() throws CpoException {
64 Connection connection;
65
66 try {
67 if (!invalidReadDataSource) {
68 connection = readDataSource.getConnection();
69 } else {
70 connection = writeDataSource.getConnection();
71 }
72 connection.setAutoCommit(false);
73 } catch (Exception e) {
74 invalidReadDataSource = true;
75
76 String msg = "getReadConnection(): failed";
77 logger.error(msg, e);
78
79 try {
80 connection = writeDataSource.getConnection();
81 connection.setAutoCommit(false);
82 } catch (SQLException e2) {
83 msg = "getWriteConnection(): failed";
84 logger.error(msg, e2);
85 throw new CpoException(msg, e2);
86 }
87 }
88
89 return connection;
90 }
91
92 @Override
93 public Connection getWriteConnection() throws CpoException {
94 Connection connection;
95
96 try {
97 connection = writeDataSource.getConnection();
98 connection.setAutoCommit(false);
99 } catch (SQLException e) {
100 String msg = "getWriteConnection(): failed";
101 logger.error(msg, e);
102 throw new CpoException(msg, e);
103 }
104
105 return connection;
106 }
107
108 @Override
109 public void closeLocalConnection(Connection connection) {
110 try {
111 if (connection != null && !connection.isClosed()) {
112 connection.close();
113 }
114 } catch (SQLException e) {
115 if (logger.isTraceEnabled()) {
116 logger.trace(e.getMessage());
117 }
118 }
119 }
120
121 @Override
122 public void commitLocalConnection(Connection connection) {
123 try {
124 if (connection != null) {
125 connection.commit();
126 }
127 } catch (SQLException e) {
128 if (logger.isTraceEnabled()) {
129 logger.trace(e.getMessage());
130 }
131 }
132 }
133
134 @Override
135 public void rollbackLocalConnection(Connection connection) {
136 try {
137 if (connection != null) {
138 connection.rollback();
139 }
140 } catch (Exception e) {
141 if (logger.isTraceEnabled()) {
142 logger.trace(e.getMessage());
143 }
144 }
145 }
146 }