def close(flush_to_disk = false)
@connection.synchronize do
begin
@connection.channel.write("closeTransaction", @txn_id,
AnalyticsLogger.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
DebugLogging.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