2323import java .util .List ;
2424
2525import com .cloud .dc .DataCenterVnetVO ;
26+ import com .cloud .utils .db .DB ;
2627import com .cloud .utils .db .GenericDao ;
2728import com .cloud .utils .db .GenericDaoBase ;
2829import com .cloud .utils .db .SearchBuilder ;
3435 * DataCenterVnetDaoImpl maintains the one-to-many relationship between
3536 * data center and the vnet that appears within its network.
3637 */
38+ @ DB (txn =false )
3739public class DataCenterVnetDaoImpl extends GenericDaoBase <DataCenterVnetVO , Long > implements GenericDao <DataCenterVnetVO , Long > {
3840 private final SearchBuilder <DataCenterVnetVO > FreeVnetSearch ;
3941 private final SearchBuilder <DataCenterVnetVO > VnetDcSearch ;
@@ -53,6 +55,7 @@ public List<DataCenterVnetVO> findVnet(long dcId, String vnet) {
5355 return listBy (sc );
5456 }
5557
58+ @ DB
5659 public void add (long dcId , int start , int end ) {
5760 String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id) VALUES ( ?, ?)" ;
5861
@@ -73,46 +76,38 @@ public void add(long dcId, int start, int end) {
7376 }
7477
7578 public void delete (long dcId ) {
76- String deleteVnet = "DELETE FROM `cloud`.`op_dc_vnet_alloc` WHERE data_center_id = ?" ;
77-
78- Transaction txn = Transaction .currentTxn ();
79- try {
80- PreparedStatement stmt = txn .prepareAutoCloseStatement (deleteVnet );
81- stmt .setLong (1 , dcId );
82- stmt .executeUpdate ();
83- } catch (SQLException e ) {
84- throw new CloudRuntimeException ("Exception caught deleting vnet " , e );
85- }
79+ SearchCriteria <DataCenterVnetVO > sc = VnetDcSearch .create ();
80+ sc .setParameters ("dc" , dcId );
81+
82+ remove (sc );
8683 }
8784
88- public DataCenterVnetVO take (long dcId , long accountId ) {
85+ @ DB
86+ public DataCenterVnetVO take (long dcId , long accountId , String reservationId ) {
8987 SearchCriteria <DataCenterVnetVO > sc = FreeVnetSearch .create ();
9088 sc .setParameters ("dc" , dcId );
9189 Date now = new Date ();
9290 Transaction txn = Transaction .currentTxn ();
93- try {
94- txn .start ();
95- DataCenterVnetVO vo = lockOneRandomRow (sc , true );
96- if (vo == null ) {
97- return null ;
98- }
99-
100- vo .setTakenAt (now );
101- vo .setAccountId (accountId );
102- update (vo .getId (), vo );
103- txn .commit ();
104- return vo ;
105-
106- } catch (Exception e ) {
107- throw new CloudRuntimeException ("Caught Exception " , e );
91+ txn .start ();
92+ DataCenterVnetVO vo = lockOneRandomRow (sc , true );
93+ if (vo == null ) {
94+ return null ;
10895 }
96+
97+ vo .setTakenAt (now );
98+ vo .setAccountId (accountId );
99+ vo .setReservationId (reservationId );
100+ update (vo .getId (), vo );
101+ txn .commit ();
102+ return vo ;
109103 }
110104
111- public void release (String vnet , long dcId , long accountId ) {
105+ public void release (String vnet , long dcId , long accountId , String reservationId ) {
112106 SearchCriteria <DataCenterVnetVO > sc = VnetDcSearchAllocated .create ();
113107 sc .setParameters ("vnet" , vnet );
114108 sc .setParameters ("dc" , dcId );
115109 sc .setParameters ("account" , accountId );
110+ sc .setParameters ("reservation" , reservationId );
116111
117112 DataCenterVnetVO vo = findOneIncludingRemovedBy (sc );
118113 if (vo == null ) {
@@ -121,6 +116,7 @@ public void release(String vnet, long dcId, long accountId) {
121116
122117 vo .setTakenAt (null );
123118 vo .setAccountId (null );
119+ vo .setReservationId (null );
124120 update (vo .getId (), vo );
125121 }
126122
@@ -146,6 +142,7 @@ protected DataCenterVnetDaoImpl() {
146142 VnetDcSearchAllocated .and ("dc" , VnetDcSearchAllocated .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
147143 VnetDcSearchAllocated .and ("taken" , VnetDcSearchAllocated .entity ().getTakenAt (), SearchCriteria .Op .NNULL );
148144 VnetDcSearchAllocated .and ("account" , VnetDcSearchAllocated .entity ().getAccountId (), SearchCriteria .Op .EQ );
145+ VnetDcSearchAllocated .and ("reservation" , VnetDcSearchAllocated .entity ().getReservationId (), SearchCriteria .Op .EQ );
149146 VnetDcSearchAllocated .done ();
150147 }
151148}
0 commit comments