# File lib/phusion_passenger/union_station/transaction.rb, line 119
        def close(flush_to_disk = false)
                @connection.synchronize do
                        return if !@connection.connected?
                        begin
                                # We need an ACK here. See thread_handler.rb finalize_request.
                                @connection.channel.write("closeTransaction", @txn_id,
                                        Core.timestamp_string, true)
                                result = @connection.channel.read
                                if result != ["ok"]
                                        raise "Expected logging agent to respond with 'ok', but got #{result.inspect} instead"
                                end
                                if flush_to_disk
                                        @connection.channel.write("flush")
                                        result = @connection.channel.read
                                        if result != ["ok"]
                                                raise "Invalid logging agent response #{result.inspect} to the 'flush' command"
                                        end
                                end
                        rescue SystemCallError, IOError => e
                                @connection.disconnect
                                warn("Error communicating with the logging agent: #{e.message}")
                        rescue Exception => e
                                @connection.disconnect
                                raise e
                        ensure
                                @connection.unref
                                @connection = nil
                        end
                end if @connection
        end