@@ -17,14 +17,22 @@ class Connection
1717 attr_accessor :isolation_level
1818
1919 def initialize config
20+ config = config . symbolize_keys
21+ @config = config
2022 @instance_id = config [ :instance ]
2123 @database_id = config [ :database ]
2224 @isolation_level = config [ :isolation_level ]
2325 @spanner = self . class . spanners config
26+ begin
27+ @mux_session = self . class . mux_sessions @spanner , config unless config [ :skip_create_multiplexed_session ]
28+ rescue Google ::Cloud ::NotFoundError
29+ @mux_session = nil
30+ rescue Google ::Cloud ::UnimplementedError
31+ @mux_session_unimplemented = true
32+ end
2433 end
2534
2635 def self . spanners config
27- config = config . symbolize_keys
2836 @spanners ||= { }
2937 @mutex ||= Mutex . new
3038 @mutex . synchronize do
@@ -40,6 +48,17 @@ def self.spanners config
4048 end
4149 end
4250
51+ def self . mux_sessions spanner , config
52+ instance_id = config [ :instance ]
53+ database_id = config [ :database ]
54+ @mux_sessions ||= { }
55+ @mutex ||= Mutex . new
56+ @mutex . synchronize do
57+ @mux_sessions [ database_path ( config ) ] ||=
58+ spanner . create_multiplexed_session instance_id , database_id
59+ end
60+ end
61+
4362 # Clears the cached information about the underlying information schemas.
4463 # Call this method if you drop and recreate a database with the same name
4564 # to prevent the cached information to be used for the new database.
@@ -96,6 +115,9 @@ def create_database
96115 job = spanner . create_database instance_id , database_id
97116 job . wait_until_done!
98117 raise Google ::Cloud ::Error . from_error job . error if job . error?
118+ unless @config [ :skip_create_multiplexed_session ] || @mux_session_unimplemented
119+ @mux_session = self . class . mux_sessions @spanner , @config
120+ end
99121 job . database
100122 end
101123
0 commit comments