@@ -563,11 +563,14 @@ def get_operation_result
563563 # if the bind was unsuccessful.
564564 def open
565565 raise LdapError . new ( "open already in progress" ) if @open_connection
566- @open_connection = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
567- @open_connection . bind @auth
568- yield self
569- @open_connection . close
570- @open_connection = nil
566+ begin
567+ @open_connection = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
568+ @open_connection . bind @auth
569+ yield self
570+ ensure
571+ @open_connection . close if @open_connection
572+ @open_connection = nil
573+ end
571574 end
572575
573576
@@ -664,14 +667,17 @@ def search args = {}
664667 }
665668 else
666669 @result = 0
667- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
668- if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
669- @result = conn . search ( args ) { |entry |
670- result_set << entry if result_set
671- yield ( entry ) if block_given?
672- }
670+ begin
671+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
672+ if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
673+ @result = conn . search ( args ) { |entry |
674+ result_set << entry if result_set
675+ yield ( entry ) if block_given?
676+ }
677+ end
678+ ensure
679+ conn . close if conn
673680 end
674- conn . close
675681 end
676682
677683 @result == 0 and result_set
@@ -743,9 +749,12 @@ def bind auth=@auth
743749 if @open_connection
744750 @result = @open_connection . bind auth
745751 else
746- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
747- @result = conn . bind auth
748- conn . close
752+ begin
753+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
754+ @result = conn . bind auth
755+ ensure
756+ conn . close if conn
757+ end
749758 end
750759
751760 @result == 0
@@ -844,11 +853,14 @@ def add args
844853 @result = @open_connection . add ( args )
845854 else
846855 @result = 0
847- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
848- if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
849- @result = conn . add ( args )
856+ begin
857+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
858+ if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
859+ @result = conn . add ( args )
860+ end
861+ ensure
862+ conn . close if conn
850863 end
851- conn . close
852864 end
853865 @result == 0
854866 end
@@ -939,11 +951,14 @@ def modify args
939951 @result = @open_connection . modify ( args )
940952 else
941953 @result = 0
942- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
943- if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
944- @result = conn . modify ( args )
954+ begin
955+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
956+ if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
957+ @result = conn . modify ( args )
958+ end
959+ ensure
960+ conn . close if conn
945961 end
946- conn . close
947962 end
948963 @result == 0
949964 end
@@ -1011,11 +1026,14 @@ def rename args
10111026 @result = @open_connection . rename ( args )
10121027 else
10131028 @result = 0
1014- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
1015- if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
1016- @result = conn . rename ( args )
1029+ begin
1030+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
1031+ if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
1032+ @result = conn . rename ( args )
1033+ end
1034+ ensure
1035+ conn . close if conn
10171036 end
1018- conn . close
10191037 end
10201038 @result == 0
10211039 end
@@ -1041,11 +1059,14 @@ def delete args
10411059 @result = @open_connection . delete ( args )
10421060 else
10431061 @result = 0
1044- conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
1045- if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
1046- @result = conn . delete ( args )
1062+ begin
1063+ conn = Connection . new ( :host => @host , :port => @port , :encryption => @encryption )
1064+ if ( @result = conn . bind ( args [ :auth ] || @auth ) ) == 0
1065+ @result = conn . delete ( args )
1066+ end
1067+ ensure
1068+ conn . close
10471069 end
1048- conn . close
10491070 end
10501071 @result == 0
10511072 end
0 commit comments