Skip to content

Commit a15bdc3

Browse files
committed
Add the possibility to interrupt connections.
1 parent a1882fd commit a15bdc3

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lib/vertica/connection.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
class Vertica::Connection
44

5-
attr_reader :options, :notices, :transaction_status, :backend_pid, :backend_key, :parameters, :notice_handler
5+
attr_reader :options, :notices, :transaction_status, :backend_pid, :backend_key, :parameters, :notice_handler, :session_id
66

77
attr_accessor :row_style, :debug
88

99
def self.cancel(existing_conn)
10-
conn = self.new(existing_conn.options.merge(:skip_startup => true))
11-
conn.write Vertica::Messages::CancelRequest.new(existing_conn.backend_pid, existing_conn.backend_key)
12-
conn.write Vertica::Messages::Flush.new
13-
conn.socket.close
10+
existing_conn.cancel
1411
end
1512

1613
# Opens a connectio the a Vertica server
@@ -92,6 +89,25 @@ def reset
9289
reset_values
9390
end
9491

92+
def cancel
93+
conn = self.class.new(options.merge(:skip_startup => true))
94+
conn.write Vertica::Messages::CancelRequest.new(backend_pid, backend_key)
95+
conn.write Vertica::Messages::Flush.new
96+
conn.socket.close
97+
end
98+
99+
def interrupt
100+
raise Vertica::Error::ConnectionError, "Session cannopt be interrupted because the session ID is not known!" if session_id.nil?
101+
conn = self.class.new(options.merge(:interruptable => false, :role => nil, :search_path => nil))
102+
response = conn.query("SELECT CLOSE_SESSION(#{Vertica.quote(session_id)})").the_value
103+
conn.close
104+
return response
105+
end
106+
107+
def interruptable?
108+
!session_id.nil?
109+
end
110+
95111
def read_message
96112
type = read_bytes(1)
97113
size = read_bytes(4).unpack('N').first
@@ -193,10 +209,12 @@ def startup_connection
193209
def initialize_connection
194210
query("SET SEARCH_PATH TO #{options[:search_path]}") if options[:search_path]
195211
query("SET ROLE #{options[:role]}") if options[:role]
212+
@session_id = query("SELECT session_id FROM v_monitor.current_session").the_value if options[:interruptable]
196213
end
197214

198215
def reset_values
199216
@parameters = {}
217+
@session_id = nil
200218
@backend_pid = nil
201219
@backend_key = nil
202220
@transaction_status = nil

test/functional/connection_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ def test_reset
6262
assert_nil @connection.backend_key
6363
assert_nil @connection.transaction_status
6464
end
65+
66+
def test_interrupt_connection
67+
@connection = Vertica::Connection.new(TEST_CONNECTION_HASH.merge(:interruptable => true))
68+
assert @connection.interruptable?
69+
end
6570

6671
def test_new_with_error_response
6772
assert_raises Vertica::Error::ConnectionError do
6873
Vertica::Connection.new(TEST_CONNECTION_HASH.merge('database' => 'nonexistant_db'))
6974
end
7075
end
71-
76+
7277
def test_connection_inspect_should_not_print_password
7378
@connection = Vertica::Connection.new(TEST_CONNECTION_HASH)
7479
inspected_string = @connection.inspect

0 commit comments

Comments
 (0)