Error while sending QUERY packet 错误
MySQL 的
max_allowed_packet太小, 在[mysqld]下修改max_allowed_packet, 然后重启MYSQL
1 | [mysqld] |
实际错误可能是
MySQL server has gone away, 设置 MySQL 的wait_timeout = 10, 并且每次操作后, 主动关闭数据库连接
wait_timeout 配置
Symfony 中的处理方式, 每次获取Repository时, 都需验证 MySQL 连接状态, 若无法ping通, 则断开重连
1 | /** |
若直接操作数据库进行了数据的修改, TCP 长连接中获取数据不是最新的
每次获取
Repository时, 调用clear()方法
1 | /** |
Order 和 User 配置了关联映射, 通过Order#User关系找到一个新实体, 该关系未配置为级联实体的持久操作
具体错误:
A new entity was found through the relationship 'Bundles\CommonBundle\Entity\Order#User' that was not configured to cascade persist operations for entity: Bundles\CommonBundle\Entity\User@000000005db8782900000000730db886. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Bundles\CommonBundle\Entity\User#__toString()' to get a clue.Order和User
1 | // 第一种, 删除 `Order` 和 `User` 的关联配置 |
开启事务后, 偶尔会提示 There is no active transaction.
EntityManager重复定义所导致, 使用单例模式
当初碰到这个错误的时是因为开启事务的时候, 实例化了 EntityManager, 当获取持久类时, getRepository 时又实例化了 EntityManager, 这是两个不同的 EntityManager, 所以偶尔会出现以上错误
开启事务后, 偶尔会提示 The entityManager is closed.
每次事务处理完后都关闭
EntityManager, 当收到新请求, 重新调用EntityManager时, 没有判断是否被关闭, EntityManager 获取方法
1 | /** |