Skip to content

Commit 511019b

Browse files
author
emiel
committed
Ensure connections are closed. Patch by Kristian Meier.
git-svn-id: http://net-ldap.rubyforge.org/svn/trunk@272 005445c4-6811-0410-8301-cae4f0071d1d
1 parent 0b06567 commit 511019b

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

History.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
=== Net::LDAP 0.0.5 / 2008-11-xx
1+
=== Net::LDAP 0.0.5 / 2009-03-xx
22

33
* 13 minor enhancements:
44
* Added Net::LDAP::Entry#to_ldif
@@ -22,7 +22,7 @@
2222
and marshalling.
2323
* Migrated to 'hoe' as the new project droid.
2424

25-
* 13 bugs fixed:
25+
* 14 bugs fixed:
2626
* Silenced some annoying warnings in filter.rb. Thanks to "barjunk"
2727
for pointing this out.
2828
* Some fairly extensive performance optimizations in the BER parser.
@@ -43,7 +43,8 @@
4343
* Allowed comma in filter strings, suggested by Kouhei.
4444
* 04Sep07, Changed four error classes to inherit from StandardError rather
4545
Exception, in order to be friendlier to irb. Suggested by Kouhei.
46-
* Minor bug fixes here and there
46+
* Ensure connections are closed. Thanks to Kristian Meier.
47+
* Minor bug fixes here and there.
4748

4849
=== Net::LDAP 0.0.4 / 2006-08-15
4950

lib/net/ldap.rb

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)