@@ -563,11 +563,14 @@ def get_operation_result
563
563
# if the bind was unsuccessful.
564
564
def open
565
565
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
571
574
end
572
575
573
576
@@ -664,14 +667,17 @@ def search args = {}
664
667
}
665
668
else
666
669
@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
673
680
end
674
- conn . close
675
681
end
676
682
677
683
@result == 0 and result_set
@@ -743,9 +749,12 @@ def bind auth=@auth
743
749
if @open_connection
744
750
@result = @open_connection . bind auth
745
751
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
749
758
end
750
759
751
760
@result == 0
@@ -844,11 +853,14 @@ def add args
844
853
@result = @open_connection . add ( args )
845
854
else
846
855
@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
850
863
end
851
- conn . close
852
864
end
853
865
@result == 0
854
866
end
@@ -939,11 +951,14 @@ def modify args
939
951
@result = @open_connection . modify ( args )
940
952
else
941
953
@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
945
961
end
946
- conn . close
947
962
end
948
963
@result == 0
949
964
end
@@ -1011,11 +1026,14 @@ def rename args
1011
1026
@result = @open_connection . rename ( args )
1012
1027
else
1013
1028
@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
1017
1036
end
1018
- conn . close
1019
1037
end
1020
1038
@result == 0
1021
1039
end
@@ -1041,11 +1059,14 @@ def delete args
1041
1059
@result = @open_connection . delete ( args )
1042
1060
else
1043
1061
@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
1047
1069
end
1048
- conn . close
1049
1070
end
1050
1071
@result == 0
1051
1072
end
0 commit comments