mysql_framework 2.1.3 → 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38653f6e3d1d849566574e58eb8386b4c32890370404cb3eade583b0070f5041
4
- data.tar.gz: 5534eca9f224d13580cfd09f4e2478208857e5b353c9a664ed7b772bf8a088e1
3
+ metadata.gz: 4e1cae192a6864318bec19e54ce0bc4db409f393493884dd7455be0809ac8bc0
4
+ data.tar.gz: d8ab5dfaf00a88efc3addb04e66c84bc8a614a9f827651a4b37581a3a71936f3
5
5
  SHA512:
6
- metadata.gz: aa4e726ac90ac8b1ecd8ca20a64b4c33e81d003c395b49c340f712df500fc7879956f5479f9c23e53fe20ca947d41d2798ad42732c596a03ab219129f02a7dbd
7
- data.tar.gz: ba2080bb5f7595b82996a48a0316ed70cf7560b5687abb9d5c9768c73f5e2b296e42378aa9139c80e2d5f705a8fa7bdb08167a740c019a6d5d0a0825436873e7
6
+ metadata.gz: 97152eedd702ad53bc27655e47eab758cd53f1e01fc756f493ebfba8e8bb618a99307c7c717d40ea4aaf331c8c5d16d5ec45dc3dc4fc36391beec50dad8a1cbe
7
+ data.tar.gz: 555e02b1f2f0a6eabb07a07a4651c39f7c1d400b843f2e21a3e6ee8e3268d4319e682c7702d0e5a2fe12ffd0e8b344ff8ab5b39adfbd7d1c60c150d385ba4fc6
@@ -4,6 +4,7 @@ module MysqlFramework
4
4
  class Connector
5
5
  def initialize(options = {})
6
6
  @options = default_options.merge(options)
7
+ @mutex = Mutex.new
7
8
 
8
9
  Mysql2::Client.default_query_options.merge!(symbolize_keys: true, cast_booleans: true)
9
10
  end
@@ -25,7 +26,7 @@ module MysqlFramework
25
26
 
26
27
  until @connection_pool.empty?
27
28
  conn = @connection_pool.pop(true)
28
- conn.close
29
+ conn&.close
29
30
  end
30
31
 
31
32
  @connection_pool = nil
@@ -38,31 +39,37 @@ module MysqlFramework
38
39
 
39
40
  # This method is called to fetch a client from the connection pool.
40
41
  def check_out
41
- return new_client unless connection_pool_enabled?
42
+ @mutex.synchronize do
43
+ begin
44
+ return new_client unless connection_pool_enabled?
42
45
 
43
- client = @connection_pool.pop(true)
46
+ client = @connection_pool.pop(true)
44
47
 
45
- client.ping if @options[:reconnect]
48
+ client.ping if @options[:reconnect]
46
49
 
47
- client
48
- rescue ThreadError
49
- if @created_connections < max_pool_size
50
- client = new_client
51
- @created_connections += 1
52
- return client
53
- end
50
+ client
51
+ rescue ThreadError
52
+ if @created_connections < max_pool_size
53
+ client = new_client
54
+ @created_connections += 1
55
+ return client
56
+ end
54
57
 
55
- MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }
58
+ MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }
56
59
 
57
- raise 'Database connection pool depleted.'
60
+ raise 'Database connection pool depleted.'
61
+ end
62
+ end
58
63
  end
59
64
 
60
65
  # This method is called to check a client back in to the connection when no longer needed.
61
66
  def check_in(client)
62
- return client&.close unless connection_pool_enabled?
67
+ @mutex.synchronize do
68
+ return client&.close unless connection_pool_enabled?
63
69
 
64
- client = new_client if client.nil? || client.closed?
65
- @connection_pool.push(client)
70
+ client = new_client if client&.closed?
71
+ @connection_pool.push(client)
72
+ end
66
73
  end
67
74
 
68
75
  # This method is called to use a client from the connection pool.
@@ -44,10 +44,10 @@ module MysqlFramework
44
44
 
45
45
  def index_exists?(client, table_name, index_name)
46
46
  result = client.query(<<~SQL)
47
- SHOW INDEX FROM #{table_name} WHERE Key_name="#{index_name}" LIMIT 1;
47
+ SHOW INDEX FROM #{table_name} WHERE Key_name="#{index_name}";
48
48
  SQL
49
49
 
50
- result.count == 1
50
+ result.count >= 1
51
51
  end
52
52
 
53
53
  protected
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MysqlFramework
4
- VERSION = '2.1.3'
4
+ VERSION = '2.1.5'
5
5
  end
@@ -118,6 +118,12 @@ describe MysqlFramework::Connector do
118
118
  end
119
119
 
120
120
  describe '#check_out' do
121
+ it 'calls synchronize on the mutex' do
122
+ expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
123
+
124
+ subject.check_out
125
+ end
126
+
121
127
  context 'when connection pooling is enabled' do
122
128
  context 'when there are available connections' do
123
129
  before do
@@ -203,6 +209,12 @@ describe MysqlFramework::Connector do
203
209
  end
204
210
 
205
211
  describe '#check_in' do
212
+ it 'calls synchronize on the mutex' do
213
+ expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
214
+
215
+ subject.check_out
216
+ end
217
+
206
218
  context 'when connection pooling is enabled' do
207
219
  it 'returns the provided client to the connection pool' do
208
220
  expect(subject.connections).to receive(:push).with(client)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-11 00:00:00.000000000 Z
11
+ date: 2023-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  requirements: []
140
- rubygems_version: 3.0.8
140
+ rubygems_version: 3.3.5
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: A lightweight framework to provide managers for working with MySQL.