显示发生故障时绑定到查询的参数 [英] Display parameters bound to query when fail happens

查看:47
本文介绍了显示发生故障时绑定到查询的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Eclipselink与MySQL结合使用.如果我的实体没有得到正确验证,并且无效数据试图泄漏到数据库中,则会引发运行时异常,并显示以下详细信息:

Using Eclipselink with MySQL. When my entities are not validated properly and invalid data tries to leak into the database, the runtime exception is being thrown with following details:

Caused by: org.springframework.transaction.TransactionSystemException: Could not commit
JPA transaction; nested exception is javax.persistence.RollbackException: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20130815-a4708b6):
org.eclipse.persistence.exceptions.DatabaseException Internal Exception:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'ip_address' at row 1
Error Code: 1406
Call: INSERT INTO statistics (action_name, created_at, ip_address, object_id, object_type, user_id) VALUES (?, ?, ?, ?, ?, ?)
     bind => [6 parameters bound]
Query: InsertObjectQuery(com.example.Statistics@3153ead4)"

我的问题是:如何从异常中发现绑定参数的值并将其保存在日志中?绑定 6个参数并没有太大帮助.

My question is: how to discover and save in logs the values of the bound parameters from the exception? The 6 parameters bound does not help much.

简单而强大的解决方案是重写实体的 toString()方法.然后它将用在日志中,显示将要保存的对象的详细信息:

Simple yet powerful solution is to override the toString() method of an entity. Then it will be used in logs, revealing details of the object that was about to be saved:

...
Call: INSERT INTO statistics (action_name, created_at, ip_address, object_id, object_type, user_id) VALUES (?, ?, ?, ?, ?, ?)
  bind => [6 parameters bound]
Query: InsertObjectQuery(Statistics{ID=null, ObjectType=PRODUCT, Action=VIEW, ObjectId=1, User=null, IP=someRandomInvalidTooLongIpAddress})"

但是,我仍然想知道是否可以设置 flag 来自动扩展 X参数绑定短语.

However, I still wonder if there is any flag I can set to expand the X parameters bound phrase automatically.

推荐答案

您可以使用eclipselink.logging.parameters持久性属性来启用参数记录:

You can enable logging of parameters using the eclipselink.logging.parameters persistence property:

<property name="eclipselink.logging.parameters" value="true"/>

请参见 http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging获取有关登录EclipseLInk的更多信息.

see http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging for more info on logging in EclipseLInk.

这篇关于显示发生故障时绑定到查询的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆