You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the database plugin, you may need to report trace information and pass a context with the request. Gorm provides the WithContext method to include a context.
See the specific [implementation details](docs/architecture.md) of the gorm plugin.
169
298
170
299
## Implementation Approach
171
300
@@ -197,14 +326,39 @@ Thus, the Client becomes a custom connection that satisfies gorm's requirements,
197
326
198
327
Additionally, there is a TxClient used for transaction handling, which implements both the ConnPool and TxCommitter interfaces defined by gorm.
199
328
329
+
## Notes
330
+
331
+
### Timeout Settings Not Effective
332
+
333
+
If users add timeout in the framework configuration, the tRPC-Go framework will create a new context when making calls and cancel the context after the call ends. This feature will cause "Context Canceled" errors when reading data from the `Row` interface, so the current plugin will set timeout to zero.
334
+
335
+
```yaml
336
+
client:
337
+
service:
338
+
- name: trpc.mysql.xxxx.xxxx # initialized as mysql
339
+
timeout: 1000# timeout configuration will not take effect
340
+
```
341
+
342
+
If you need to configure request timeout, you can use context.WithTimeout() to control the context yourself.
343
+
If you want to configure connection establishment timeout, connection read/write timeout, you can consider configuring related parameters in the DSN, for example, for MySQL you can configure [`readTimeout`, `writeTimeout` and `timeout`](https://github.com/go-sql-driver/mysql?tab=readme-ov-file#connection-pool-and-timeouts) in the DSN.
344
+
345
+
```yaml
346
+
client:
347
+
service:
348
+
- name: trpc.mysql.server.service
349
+
# Add readTimeout to DSN parameters, reference: https://github.com/go-sql-driver/mysql?tab=readme-ov-file#dsn-data-source-name
* Custom Connection in GORM:https://gorm.io/zh_CN/docs/connecting_to_the_database.html
204
356
205
357
## FAQ
358
+
206
359
### How to print specific SQL statements and results?
207
-
This plugin has implemented the TRPC Logger for GORM, as mentioned in the "Logging" section above. If you are using the default NewClientProxy, you can use the Debug() method before the request to output the SQL statements to the TRPC log with the Info level.
360
+
This plugin has implemented the TRPC Logger for GORM. You only need to configure `plugin.database.gorm.logger` configuration (requires plugin version >= v0.2.2) to print specific SQL statements to tRPC-Go logs.
361
+
Or you can add `Debug()` before the request to output to logs at Info level.
### How to set the isolation level for transactions?
223
377
When starting a transaction, you can provide sql.TxOptions in the Begin method to set the isolation level. Alternatively, you can set it manually after starting the transaction using tx.Exec.
224
378
379
+
### Soft delete not working after switching from native gorm to tRPC gorm plugin
380
+
381
+
This is because gorm changed the soft delete method after the major version upgrade from jinzhu/gorm to gorm.io/gorm.
382
+
383
+
For the new soft delete method, see the documentation https://gorm.io/docs/delete.html#Soft-Delete
384
+
385
+
Using gorm.DeleteAt can directly be compatible with jinzhu/gorm soft delete.
386
+
387
+
### How to handle Context Canceled errors
388
+
389
+
**Update to the latest version to resolve this issue. The latest version will force timeout to 0.**
390
+
391
+
This problem is generally caused by setting a global timeout in the trpc framework client. A common trpc framework configuration example is as follows:
392
+
393
+
```yaml
394
+
client:
395
+
timeout: 1000 # Need to remove this
396
+
service:
397
+
- name: xxxxx
398
+
protocol: trpc
399
+
timeout: 1000 # Set timeout separately for other services
400
+
```
401
+
402
+
The client-level timeout is a global timeout. All trpc clients that don't have a separate timeout setting will use this setting. The old version of this plugin would report errors due to context being canceled as long as timeout was set.
403
+
225
404
### Currently, only MySQL, ClickHouse, and PostgreSQL are supported.
226
405
406
+
Parse the `client.service.name` name, which needs to use the standard four-segment name format. For example, `trpc.mysql.xxx.xxx` initializes the `mysql driver`; `trpc.clickhouse.xxx.xxx` initializes the `clickhouse driver`; `trpc.postgres.xxx.xxx` initializes the `postgres driver(pgx)`.
407
+
408
+
For non-standard four-segment names, it defaults to initializing the `mysql driver`.
409
+
227
410
### When using GORM transactions, only the Begin method goes through the tRPC call chain.
411
+
412
+
**Update to the latest version to resolve this issue. v0.1.3 version has resolved this issue.**
413
+
228
414
This is a design compromise made to reduce complexity. In this plugin, when calling BeginTx, it directly returns the result of sql.DB.BeginTx(), which is an already opened transaction. Subsequent transaction operations are handled by that transaction.
229
415
230
416
Considering that this plugin is mainly designed for connecting to MySQL instances, this approach can reduce some complexity while ensuring normal operation. However, considering that there may be services that require all database requests to go through the tRPC filter, the mechanism will be modified in the future to make all requests within the transaction go through the tRPC request flow.
231
417
232
418
If inaccurate reporting of data is caused by this behavior, you can disable the GORM transaction optimization to ensure that all requests that do not explicitly use transactions go through the basic reporting methods.
233
419
420
+
> To ensure data consistency, GORM performs write operations (create, update, delete) within transactions. If you don't have this requirement, you can disable it during initialization.
0 commit comments