<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -107,13 +107,14 @@ module ActiveRecord
         checkin conn if conn
       end
 
-      # Reserve a connection, and yield it to a block. Ensure the connection is
-      # checked back in when finished.
+      # If a connection already exists yield it to the block.  If no connection
+      # exists checkout a connection, yield it to the block, and checkin the 
+      # connection when finished.
       def with_connection
-        conn = checkout
-        yield conn
+        fresh_connection = true unless @reserved_connections[current_connection_id]
+        yield connection
       ensure
-        checkin conn
+        release_connection if fresh_connection
       end
 
       # Returns true if a connection has already been opened.</diff>
      <filename>activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,6 @@
 require &quot;cases/helper&quot;
+require &quot;models/project&quot;
+require &quot;timeout&quot;
 
 class PooledConnectionsTest &lt; ActiveRecord::TestCase
   def setup
@@ -89,6 +91,33 @@ class PooledConnectionsTest &lt; ActiveRecord::TestCase
   ensure
     ActiveRecord::Base.connection_handler = old_handler
   end
+
+  def test_with_connection_nesting_safety
+    ActiveRecord::Base.establish_connection(@connection.merge({:pool =&gt; 1, :wait_timeout =&gt; 0.1}))
+
+    before_count = Project.count
+
+    add_record('one')
+
+    ActiveRecord::Base.connection.transaction do
+      add_record('two')
+      # Have another thread try to screw up the transaction
+      Thread.new do
+        raise ActiveRecord::Rollback
+        ActiveRecord::Base.connection_pool.release_connection
+      end.join rescue nil
+      add_record('three')
+    end
+
+    after_count = Project.count
+    assert_equal 3, after_count - before_count
+  end
+
+  private
+
+  def add_record(name)
+    ActiveRecord::Base.connection_pool.with_connection { Project.create! :name =&gt; name }
+  end
 end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
 
 class AllowConcurrencyDeprecatedTest &lt; ActiveRecord::TestCase</diff>
      <filename>activerecord/test/cases/pooled_connections_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b193f233908823cccd2f1d5fcb4146787ed8c4ed</id>
    </parent>
  </parents>
  <author>
    <name>steve</name>
    <email>coderrr.contact@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/5501b99a19a2a67a9a920fd3c7bff071a2ecf058</url>
  <id>5501b99a19a2a67a9a920fd3c7bff071a2ecf058</id>
  <committed-date>2009-05-01T06:58:10-07:00</committed-date>
  <authored-date>2009-05-01T06:58:10-07:00</authored-date>
  <message>Ensure ActiveRecord::Base.connection_pool.with_connection creates a new connection only when needed [#1752 state:resolved]

Signed-off-by: Pratik Naik &lt;pratiknaik@gmail.com&gt;</message>
  <tree>801cbec74d31e7600c172a5146a30a3953f61cc0</tree>
  <committer>
    <name>Pratik Naik</name>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
