@@ -96,7 +96,7 @@ defmodule MyXQL do
96
96
the prepare statement count of your databases (such as using a dashboard or
97
97
setting alarm handlers)
98
98
99
- * `:disconnect_on_error_codes` - List of error code atoms that when encountered
99
+ * `:disconnect_on_error_codes` - List of error code integers or atoms that when encountered
100
100
will disconnect the connection. See "Disconnecting on Errors" section below for more
101
101
information.
102
102
@@ -129,11 +129,11 @@ defmodule MyXQL do
129
129
iex> {:ok, pid} = MyXQL.start_link(after_connect: &MyXQL.query!(&1, "SET time_zone = '+00:00'"))
130
130
{:ok, #PID<0.69.0>}
131
131
132
- ## Disconnecting on Errors
132
+ ## Disconnecting on errors
133
133
134
- Sometimes the connection becomes unusable. For example, some services, such as AWS Aurora,
135
- support failover. This means the database you are currently connected to may suddenly become
136
- read-only, and an attempt to do any write operation, such as INSERT/UPDATE/DELETE will lead to
134
+ Sometimes the connection becomes unusable. For example, services such as AWS Aurora support
135
+ failover which means the database you are currently connected to may suddenly become
136
+ read-only. An attempt to do any write operation, such as INSERT/UPDATE/DELETE will lead to
137
137
errors such as:
138
138
139
139
** (MyXQL.Error) (1792) (ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION) Cannot execute statement in a READ ONLY transaction.
@@ -147,18 +147,35 @@ defmodule MyXQL do
147
147
MyXQL automatically disconnects the connection on the following error codes and they don't have
148
148
to be configured:
149
149
150
- * `ER_MAX_PREPARED_STMT_COUNT_REACHED`
150
+ * `: ER_MAX_PREPARED_STMT_COUNT_REACHED`
151
151
152
- To convert error code number to error code name you can use `perror` command-line utility that
153
- ships with MySQL client installation, e.g.:
152
+ You can pass error codes as integers too:
153
+
154
+ disconnect_on_error_codes: [1792]
155
+
156
+ ## Error codes
157
+
158
+ MyXQL maintains a mapping of integers/atoms for commonly used errors. You can add additional
159
+ ones by adding the following compile-time configuration:
160
+
161
+ config :myxql, :extra_error_codes, [
162
+ {1048, :ER_BAD_NULL_ERROR}
163
+ ]
164
+
165
+ After adding the configuration, MyXQL needs to be recompiled. It can be done with:
166
+
167
+ $ mix deps.clean myxql --build
168
+
169
+ To convert error code integers to names you can use `perror` command-line utility that ships
170
+ with MySQL client installation, e.g.:
154
171
155
172
bash$ perror 1792
156
173
MySQL error code 1792 (ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION): Cannot execute statement in a READ ONLY transaction.
157
174
158
175
"""
159
176
@ spec start_link ( [ start_option ( ) ] ) :: { :ok , pid ( ) } | { :error , MyXQL.Error . t ( ) }
160
177
def start_link ( options ) do
161
- ensure_deps_started !( options )
178
+ options = ensure_valid_error_codes !( options )
162
179
DBConnection . start_link ( MyXQL.Connection , options )
163
180
end
164
181
@@ -526,9 +543,9 @@ defmodule MyXQL do
526
543
Returns a supervisor child specification for a DBConnection pool.
527
544
"""
528
545
@ spec child_spec ( [ start_option ( ) ] ) :: :supervisor . child_spec ( )
529
- def child_spec ( opts ) do
530
- ensure_deps_started! ( opts )
531
- DBConnection . child_spec ( MyXQL.Connection , opts )
546
+ def child_spec ( options ) do
547
+ options = ensure_valid_error_codes! ( options )
548
+ DBConnection . child_spec ( MyXQL.Connection , options )
532
549
end
533
550
534
551
@ doc """
@@ -547,18 +564,37 @@ defmodule MyXQL do
547
564
548
565
## Helpers
549
566
550
- defp ensure_deps_started! ( opts ) do
551
- if Keyword . get ( opts , :ssl , false ) and
552
- not List . keymember? ( :application . which_applications ( ) , :ssl , 0 ) do
553
- raise """
554
- SSL connection cannot be established because `:ssl` application is not started,
555
- you can add it to `:extra_applications` in your `mix.exs`:
567
+ defp ensure_valid_error_codes! ( opts ) do
568
+ default_error_codes = [
569
+ :ER_MAX_PREPARED_STMT_COUNT_REACHED
570
+ ]
556
571
557
- def application() do
558
- [extra_applications: [:ssl]]
572
+ codes = default_error_codes ++ Keyword . get ( opts , :disconnect_on_error_codes , [ ] )
573
+
574
+ codes =
575
+ for code <- codes do
576
+ if is_integer ( code ) do
577
+ code
578
+ else
579
+ integer = MyXQL.Protocol.ServerErrorCodes . name_to_code ( code )
580
+
581
+ unless integer do
582
+ raise """
583
+ #{ inspect ( code ) } is not a recognized error code
584
+
585
+ To solve this, you can either:
586
+
587
+ - pass an integer error code
588
+
589
+ - make it recognizable by adding it to `config :myxql, extra_error_codes: [...]`
590
+ (See "Error codes" in MyXQL.start_link/1 documentation for more information).
591
+ """
559
592
end
560
593
561
- """
562
- end
594
+ integer
595
+ end
596
+ end
597
+
598
+ Keyword . put ( opts , :disconnect_on_error_codes , codes )
563
599
end
564
600
end
0 commit comments