1 package org.kuali.coeus.dc.common.db;
2
3 import java.sql.*;
4
5 public abstract class AbstractConnectionDaoService implements ConnectionDaoService {
6
7 private String riceConnectionString;
8 private String coeusConnectionString;
9 private String riceUser;
10 private String coeusUser;
11 private String ricePassword;
12 private String coeusPassword;
13
14 private static final ThreadLocal<Connection> COEUS_CONNECTIONS = new ThreadLocal<>();
15 private static final ThreadLocal<Connection> RICE_CONNECTIONS = new ThreadLocal<>();
16
17
18 {
19 try {
20 Class.forName(getDriverClassName()).newInstance();
21 } catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) {
22 throw new RuntimeException(e);
23 }
24 }
25
26 public abstract String getDriverClassName();
27
28 @Override
29 public Connection getCoeusConnection() {
30 Connection c = COEUS_CONNECTIONS.get();
31 if (c == null) {
32 try {
33 c = getConnection(getCoeusConnectionString(), getCoeusUser(), getCoeusPassword());
34 c.setAutoCommit(false);
35 } catch (SQLException e) {
36 e.printStackTrace();
37 throw new RuntimeException(e);
38 }
39
40 COEUS_CONNECTIONS.set(c);
41 }
42
43 return c;
44 }
45
46 @Override
47 public Connection getRiceConnection() {
48 Connection c = RICE_CONNECTIONS.get();
49 if (c == null) {
50 try {
51 c = getConnection(getRiceConnectionString(),getRiceUser(),getRicePassword());
52 c.setAutoCommit(false);
53 } catch (SQLException e) {
54 throw new RuntimeException(e);
55 }
56
57 RICE_CONNECTIONS.set(c);
58 }
59
60 return c;
61 }
62
63 private Connection getConnection(String con, String user, String pass) throws SQLException {
64 if (user != null && !user.trim().equals("")) {
65 return DriverManager.getConnection(con, user, pass);
66 } else {
67 return DriverManager.getConnection(con);
68 }
69 }
70
71 public String getRiceConnectionString() {
72 return riceConnectionString;
73 }
74
75 public void setRiceConnectionString(String riceConnectionString) {
76 this.riceConnectionString = riceConnectionString;
77 }
78
79 public String getCoeusConnectionString() {
80 return coeusConnectionString;
81 }
82
83 public void setCoeusConnectionString(String coeusConnectionString) {
84 this.coeusConnectionString = coeusConnectionString;
85 }
86
87 private String getRiceUser() {
88 return riceUser;
89 }
90
91 public void setRiceUser(String riceUser) {
92 this.riceUser = riceUser;
93 }
94
95 private String getCoeusUser() {
96 return coeusUser;
97 }
98 public void setCoeusUser(String coeusUser) {
99 this.coeusUser = coeusUser;
100 }
101 private String getRicePassword() {
102 return ricePassword;
103 }
104 public void setRicePassword(String ricePassword) {
105 this.ricePassword = ricePassword;
106 }
107 private String getCoeusPassword() {
108 return coeusPassword;
109 }
110 public void setCoeusPassword(String coeusPassword) {
111 this.coeusPassword = coeusPassword;
112 }
113 }