Mercurial > dive4elements > gnv-client
comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/connectionpool/ArcSDEPoolableObjectFactory.java @ 553:5b536542ef56
Another attempt to fix gnv/issue34. Implemented an internal idle time checking.
geo-backend/trunk@619 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 25 Jan 2010 12:12:31 +0000 |
parents | 1f6e2b256247 |
children | 12f88239fb33 |
comparison
equal
deleted
inserted
replaced
552:7615ee5d1345 | 553:5b536542ef56 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.geobackend.sde.connectionpool; | 1 package de.intevation.gnv.geobackend.sde.connectionpool; |
2 | |
3 import de.intevation.gnv.geobackend.base.connectionpool.exception.ConnectionException; | |
4 | |
5 import de.intevation.gnv.geobackend.sde.datasources.ArcSDEConnection; | |
5 | 6 |
6 import java.sql.Connection; | 7 import java.sql.Connection; |
7 import java.sql.SQLException; | 8 import java.sql.SQLException; |
9 | |
8 import java.util.Properties; | 10 import java.util.Properties; |
9 | 11 |
10 import org.apache.commons.pool.PoolableObjectFactory; | 12 import org.apache.commons.pool.PoolableObjectFactory; |
13 | |
11 import org.apache.log4j.Logger; | 14 import org.apache.log4j.Logger; |
12 | 15 |
13 import de.intevation.gnv.geobackend.base.connectionpool.exception.ConnectionException; | |
14 import de.intevation.gnv.geobackend.sde.datasources.ArcSDEConnection; | |
15 | |
16 /** | 16 /** |
17 * @author Tim Englich <tim.englich@intevation.de> | 17 * @author Tim Englich (tim.englich@intevation.de) |
18 * | 18 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) |
19 */ | 19 */ |
20 public class ArcSDEPoolableObjectFactory implements PoolableObjectFactory { | 20 public class ArcSDEPoolableObjectFactory implements PoolableObjectFactory { |
21 | 21 |
22 /** | 22 /** |
23 * the logger, used to log exceptions and additonaly information | 23 * the logger, used to log exceptions and additonaly information |
24 */ | 24 */ |
25 private static Logger log = Logger.getLogger(ArcSDEPoolableObjectFactory.class); | 25 private static Logger log = Logger.getLogger( |
26 | 26 ArcSDEPoolableObjectFactory.class); |
27 private int serverRoundtripInterval = 1000 * 30;// 30 Sekunden | 27 |
28 // The 5 seconds are inspired by GeoTools's testServer() usage. | |
29 private int serverRoundtripInterval = 5; | |
30 | |
31 private long serverInactiveInterval = 5L*60L*1000L; // 5 minutes | |
28 /** | 32 /** |
29 * The URL to the ArcSDE Server | 33 * The URL to the ArcSDE Server |
30 */ | 34 */ |
31 private String server = null; | 35 private String server = null; |
32 /** | 36 /** |
49 /** | 53 /** |
50 * Constructor of this Class | 54 * Constructor of this Class |
51 * @param properties the Properties which includes the ConnectionParams to the Database | 55 * @param properties the Properties which includes the ConnectionParams to the Database |
52 */ | 56 */ |
53 public ArcSDEPoolableObjectFactory(Properties properties) { | 57 public ArcSDEPoolableObjectFactory(Properties properties) { |
58 | |
54 log.debug("ArcSDEPoolableObjectFactory.Constructor"); | 59 log.debug("ArcSDEPoolableObjectFactory.Constructor"); |
55 this.server = properties.getProperty("server"); | 60 |
56 this.port = properties.getProperty("port"); | 61 server = properties.getProperty("server"); |
57 this.database = properties.getProperty("database"); | 62 port = properties.getProperty("port"); |
58 this.username = properties.getProperty("username"); | 63 database = properties.getProperty("database"); |
59 this.credentials = properties.getProperty("credentials"); | 64 username = properties.getProperty("username"); |
65 credentials = properties.getProperty("credentials"); | |
66 | |
67 String serverRoundtripIntervalValue = | |
68 properties.getProperty("serverRoundtripInterval"); | |
69 String serverInactiveIntervalValue = | |
70 properties.getProperty("serverInactiveInterval"); | |
60 | 71 |
61 try { | 72 try { |
62 String serverRoundtripIntervalValue = properties.getProperty("serverRoundtripInterval"); | 73 if (serverRoundtripIntervalValue != null) { |
63 if (serverRoundtripIntervalValue != null){ | 74 serverRoundtripInterval = |
64 this.serverRoundtripInterval = Integer.parseInt(serverRoundtripIntervalValue); | 75 Integer.parseInt(serverRoundtripIntervalValue); |
65 } | 76 } |
66 } catch (NumberFormatException e) { | 77 } |
78 catch (NumberFormatException e) { | |
79 log.error(e,e); | |
80 } | |
81 | |
82 try { | |
83 if (serverInactiveIntervalValue != null) { | |
84 serverInactiveInterval = 1000L * // input in seconds! | |
85 Long.parseLong(serverInactiveIntervalValue); | |
86 } | |
87 } | |
88 catch (NumberFormatException e) { | |
67 log.error(e,e); | 89 log.error(e,e); |
68 } | 90 } |
69 | 91 |
70 log.info("ArcSDEPoolableObjectFactory initialized"); | 92 log.info("ArcSDEPoolableObjectFactory initialized"); |
71 log.info("Server: "+this.server); | 93 log.info("Server: " + server); |
72 log.info("Port: "+this.port); | 94 log.info("Port: " + port); |
73 log.info("Database: "+this.database); | 95 log.info("Database: " + database); |
74 log.info("User: "+this.username); | 96 log.info("User: " + username); |
75 log.info("Testtimeout: "+this.serverRoundtripInterval); | 97 log.info("Roundtrip check interval: " + serverRoundtripInterval); |
76 | 98 log.info("Inactive check interval: " + serverInactiveInterval); |
77 } | 99 } |
78 | 100 |
79 /** | 101 /** |
80 * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object) | 102 * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object) |
81 */ | 103 */ |
82 public void activateObject(Object arg0) throws Exception { | 104 public void activateObject(Object arg0) throws Exception { |
100 */ | 122 */ |
101 public Object makeObject() throws Exception { | 123 public Object makeObject() throws Exception { |
102 log.debug("ArcSDEPoolableObjectFactory.makeObject"); | 124 log.debug("ArcSDEPoolableObjectFactory.makeObject"); |
103 Connection con; | 125 Connection con; |
104 try { | 126 try { |
105 con = new ArcSDEConnection(this.server, this.port, this.database, this.username, this.credentials, this.serverRoundtripInterval); | 127 con = new ArcSDEConnection( |
128 server, | |
129 port, | |
130 database, | |
131 username, | |
132 credentials, | |
133 serverRoundtripInterval, | |
134 serverInactiveInterval); | |
106 } | 135 } |
107 catch (ConnectionException e) { | 136 catch (ConnectionException e) { |
108 throw new ConnectionException("Establishing a connection to database failed: " + e.toString(), e); | 137 throw new ConnectionException( |
138 "Establishing a connection to database failed: " + | |
139 e.toString(), e); | |
109 } | 140 } |
110 return con; | 141 return con; |
111 } | 142 } |
112 | 143 |
113 /** | 144 /** |
114 * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object) | 145 * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object) |
115 */ | 146 */ |
116 public void passivateObject(Object arg0) throws Exception { | 147 public void passivateObject(Object arg0) throws Exception { |
117 log.debug("ArcSDEPoolableObjectFactory.passivateObject"); | 148 |
149 boolean debug = log.isDebugEnabled(); | |
150 | |
151 if (debug) { | |
152 log.debug("ArcSDEPoolableObjectFactory.passivateObject"); | |
153 } | |
154 | |
155 if (arg0 instanceof ArcSDEConnection) { | |
156 if (debug) { | |
157 log.debug(" touching connection"); | |
158 } | |
159 ((ArcSDEConnection)arg0).touch(); | |
160 } | |
118 } | 161 } |
119 | 162 |
120 /** | 163 /** |
121 * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object) | 164 * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object) |
122 */ | 165 */ |
123 public boolean validateObject(Object arg0) { | 166 public boolean validateObject(Object arg0) { |
124 log.debug("ArcSDEPoolableObjectFactory.validateObject"); | 167 |
125 | 168 boolean debug = log.isDebugEnabled(); |
126 boolean returnValue = false; | 169 |
170 if (debug) { | |
171 log.debug("ArcSDEPoolableObjectFactory.validateObject"); | |
172 } | |
173 | |
174 if (!(arg0 instanceof ArcSDEConnection)) { | |
175 return false; | |
176 } | |
177 | |
127 try { | 178 try { |
128 returnValue = arg0 instanceof ArcSDEConnection | 179 ArcSDEConnection con = (ArcSDEConnection)arg0; |
129 ? ((ArcSDEConnection)arg0).isValid(this.serverRoundtripInterval) | 180 |
130 : false; | 181 boolean isValid = |
182 con.isActive() && con.isValid(serverRoundtripInterval); | |
183 | |
184 if (!isValid && debug) { | |
185 log.debug("connection is invalid!"); | |
186 } | |
187 | |
188 return isValid; | |
131 } | 189 } |
132 catch (SQLException sqle) {} | 190 catch (SQLException sqle) { |
133 return returnValue; | 191 log.error(sqle, sqle); |
192 } | |
193 | |
194 return false; | |
134 } | 195 } |
135 } | 196 } |