1818
1919package com .cloud .cluster .dao ;
2020
21- import java .sql .Connection ;
2221import java .sql .PreparedStatement ;
2322import java .sql .SQLException ;
2423import java .util .Date ;
@@ -51,110 +50,20 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
5150 private final SearchBuilder <ManagementServerHostVO > StateSearch ;
5251
5352 @ Override
54- public void update (Connection conn , long id , long runid , String name , String version , String serviceIP , int servicePort , Date lastUpdate ) {
53+ public void invalidateRunSession (long id , long runid ) {
54+ Transaction txn = Transaction .currentTxn ();
5555 PreparedStatement pstmt = null ;
5656 try {
57- pstmt = conn .prepareStatement ("update mshost set name=?, version=?, service_ip=?, service_port=?, last_update=?, removed=null, alert_count=0, runid=? where id=?" );
58- pstmt .setString (1 , name );
59- pstmt .setString (2 , version );
60- pstmt .setString (3 , serviceIP );
61- pstmt .setInt (4 , servicePort );
62- pstmt .setString (5 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), lastUpdate ));
63- pstmt .setLong (6 , runid );
64- pstmt .setLong (7 , id );
65-
66- pstmt .executeUpdate ();
67- conn .commit ();
68- } catch (SQLException e ) {
69- throw new CloudRuntimeException ("DB exception on " + pstmt .toString (), e );
70- } finally {
71- if (pstmt != null ) {
72- try {
73- pstmt .close ();
74- } catch (Exception e ) {
75- s_logger .warn ("Unable to close prepared statement due to exception " , e );
76- }
77- }
78- }
79- }
80-
81- @ Override
82- public void update (Connection conn , long id , long runid , Date lastUpdate ) {
83- PreparedStatement pstmt = null ;
84- try {
85- pstmt = conn .prepareStatement ("update mshost set last_update=?, removed=null, alert_count=0 where id=? and runid=?" );
86- pstmt .setString (1 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), lastUpdate ));
87- pstmt .setLong (2 , id );
88- pstmt .setLong (3 , runid );
89-
90- int count = pstmt .executeUpdate ();
91- conn .commit ();
92-
93- if (count < 1 ) {
94- throw new CloudRuntimeException ("Invalid cluster session detected" , new ClusterInvalidSessionException ("runid " + runid + " is no longer valid" ));
95- }
96- } catch (SQLException e ) {
97- throw new CloudRuntimeException ("DB exception on " + pstmt .toString (), e );
98- } finally {
99- if (pstmt != null ) {
100- try {
101- pstmt .close ();
102- } catch (Exception e ) {
103- s_logger .warn ("Unable to close prepared statement due to exception " , e );
104- }
105- }
106- }
107- }
108-
109- @ Override
110- public void invalidateRunSession (Connection conn , long id , long runid ) {
111- PreparedStatement pstmt = null ;
112- try {
113- pstmt = conn .prepareStatement ("update mshost set runid=0, state='Down' where id=? and runid=?" );
57+ pstmt = txn .prepareAutoCloseStatement ("update mshost set runid=0, state='Down' where id=? and runid=?" );
11458 pstmt .setLong (1 , id );
11559 pstmt .setLong (2 , runid );
11660
11761 pstmt .executeUpdate ();
118- conn .commit ();
11962 } catch (SQLException e ) {
12063 throw new CloudRuntimeException ("DB exception on " + pstmt .toString (), e );
121- } finally {
122- if (pstmt != null ) {
123- try {
124- pstmt .close ();
125- } catch (Exception e ) {
126- s_logger .warn ("Unable to close prepared statement due to exception " , e );
127- }
128- }
12964 }
13065 }
13166
132- @ Override
133- public List <ManagementServerHostVO > getActiveList (Connection conn , Date cutTime ) {
134- Transaction txn = Transaction .openNew ("getActiveList" , conn );
135- try {
136- SearchCriteria <ManagementServerHostVO > sc = ActiveSearch .create ();
137- sc .setParameters ("lastUpdateTime" , cutTime );
138-
139- return listIncludingRemovedBy (sc );
140- } finally {
141- txn .close ("getActiveList" );
142- }
143- }
144-
145- @ Override
146- public List <ManagementServerHostVO > getInactiveList (Connection conn , Date cutTime ) {
147- Transaction txn = Transaction .openNew ("getInactiveList" , conn );
148- try {
149- SearchCriteria <ManagementServerHostVO > sc = InactiveSearch .create ();
150- sc .setParameters ("lastUpdateTime" , cutTime );
151-
152- return listIncludingRemovedBy (sc );
153- } finally {
154- txn .close ("getInactiveList" );
155- }
156- }
157-
15867 @ Override
15968 public ManagementServerHostVO findByMsid (long msid ) {
16069 SearchCriteria <ManagementServerHostVO > sc = MsIdSearch .create ();
@@ -190,7 +99,6 @@ public void update(long id, long runid, String name, String version, String serv
19099 txn .commit ();
191100 } catch (Exception e ) {
192101 s_logger .warn ("Unexpected exception, " , e );
193- txn .rollback ();
194102 }
195103 }
196104
@@ -210,7 +118,6 @@ public boolean remove(Long id) {
210118 return true ;
211119 } catch (Exception e ) {
212120 s_logger .warn ("Unexpected exception, " , e );
213- txn .rollback ();
214121 }
215122
216123 return false ;
@@ -237,7 +144,6 @@ public void update(long id, long runid, Date lastUpdate) {
237144 }
238145 } catch (Exception e ) {
239146 s_logger .warn ("Unexpected exception, " , e );
240- txn .rollback ();
241147 }
242148 }
243149
@@ -298,31 +204,23 @@ protected ManagementServerHostDaoImpl() {
298204
299205
300206 @ Override
301- public void update (Connection conn , long id , long runId , State state , Date lastUpdate ) {
207+ public void update (long id , long runId , State state , Date lastUpdate ) {
208+ Transaction txn = Transaction .currentTxn ();
302209 PreparedStatement pstmt = null ;
303210 try {
304- pstmt = conn . prepareStatement ("update mshost set state=?, last_update=? where id=? and runid=?" );
211+ pstmt = txn . prepareAutoCloseStatement ("update mshost set state=?, last_update=? where id=? and runid=?" );
305212 pstmt .setString (1 , state .toString ());
306213 pstmt .setString (2 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), lastUpdate ));
307214 pstmt .setLong (3 , id );
308215 pstmt .setLong (4 , runId );
309216
310217 int count = pstmt .executeUpdate ();
311- conn .commit ();
312218
313219 if (count < 1 ) {
314220 throw new CloudRuntimeException ("Invalid cluster session detected" , new ClusterInvalidSessionException ("runid " + runId + " is no longer valid" ));
315221 }
316222 } catch (SQLException e ) {
317223 throw new CloudRuntimeException ("DB exception on " + pstmt .toString (), e );
318- } finally {
319- if (pstmt != null ) {
320- try {
321- pstmt .close ();
322- } catch (Exception e ) {
323- s_logger .warn ("Unable to close prepared statement due to exception " , e );
324- }
325- }
326224 }
327225 }
328226
0 commit comments