我在Spring Data JPA中尝试从MySQL数据库中读取(选择)实体对象时遇到以下异常。
SQL错误:1792,SQLState:25006
无法在READ ONLY事务中执行语句。
这是完整的日志:
INFO] 2018-04-11 10:10:47.378 [http-nio-127.0.0.1-8080-exec-1] OrganizationServiceImpl - 按ID:11408
查找组织休眠:选择organizati0_.id为id1_12_0_,organizati0_.address_1为address_2_12_0_,organizati0_.address_2为address_3_12_0_,organizati0_.circulation为circulat4_12_0_,organizati0_.city为city5_12_0_,organizati0_.country为country6_12_0_,organizati0_.created_by为created20_12_0_,organizati0_。 created_on as created_7_12_0_,organizati0_.demographics as demograp8_12_0_,organizati0_.fax as fax9_12_0_,organizati0_.frequency as frequent10_12_0_,organizati0_.medium as medium11_12_0_,organizati0_.modified_by as modifie21_12_0_,organinati0_.modified_on as modifie12_12_0_,organorganati_.name as name13_12_0_,organinati0_.notes as notes14_12_0_,organizati0_.phone as phone15_12_0_,organizati0_.state as state16_12_0_,organizati0_.version as version17_12_0_,organizati0_.website as website18_12_0_,organizati0_.zip as zip19_12_0_ from organization organizati0_ where organizati0_.id =?更新
2018-04-11 10:10:47.381 WARN 3248 --- [0.1-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1792,SQLState:25006 2018-04-11 10:10:47.382 ERROR 3248 --- [0.1-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:无法在READ ONLY事务中执行语句。 2018-04-11 10:10:47.382 INFO 3248 --- [0.1-8080-exec-1] o.h.e.internal.DefaultLoadEventListener:HHH000327:执行加载命令时出错:org.hibernate.exception.GenericJDBCException:无法提取ResultSet 2018-04-11 10:10:47.390 ERROR 3248 --- [0.1-8080-exec-1] osboot.web.support.ErrorPageFilter:由于异常而转发到请求[/ organizations / edit]的错误页面[可能]不提取ResultSet;嵌套异常是org.hibernate.exception.GenericJDBCException:无法提取ResultSet]
org.springframework.orm.jpa.JpaSystemException:无法提取ResultSet;嵌套异常是org.hibernate.exception.GenericJDBCException:无法提取ResultSet
...
引起:org.hibernate.exception.GenericJDBCException:无法提取ResultSet 在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4019)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.lockAndLoad(DefaultLoadEventListener.java:403)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:124)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.internal.SessionImpl.access $ 2600(SessionImpl.java:164)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.load(SessionImpl.java:2689)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.internal.SessionImpl.get(SessionImpl.java:1082)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1069)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_91] 在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_91] 在org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)〜[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE] at com.sun.proxy。$ Proxy118.find(Unknown Source)〜[na:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_91] at java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_91]
...
引起:java.sql.SQLException:无法在READ ONLY事务中执行语句。 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)〜[mysql-connector-java-5.1.44.jar:5.1.44] 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)〜[mysql-connector-java-5.1.44.jar:5.1.44] 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)〜[mysql-connector-java-5.1.44.jar:5.1.44] 在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)〜[mysql-connector-java-5.1.44.jar:5.1.44] 在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)〜[mysql-connector-java-5.1.44.jar:5.1.44] 在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)〜[mysql-connector-java-5.1.44.jar:5.1.44]
你可以看到所有Hibernate试图做的都是选择。事务是只读的并不重要,事实上,它应该是只读的。
这是我的实体:
@Entity
@Table(name="organization")
public class Organization implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="ID")
private Long org_id;
@Column(name="NAME")
@NotEmpty(message = "Please provide a name")
private String name;
@Column(name="ADDRESS_1")
private String address1;
以下是服务类的相关部分:
@Repository
@Transactional(readOnly = true)
public class OrganizationServiceImpl implements OrganizationService {
private static Logger log = LogManager.getLogger();
@Autowired
OrganizationRepository repo;
@Override
public Organization findOne(Long id) {
log.info("Finding organization by id: " + id);
return repo.findOne(id);
}
存储库界面:
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
/**
* Returns an Organization object and locks the record.
*
* @param id
* @return
*/
@Lock(LockModeType.PESSIMISTIC_WRITE)
Organization findOne(Long id);
我尝试将其更改为@Transactional(readOnly = false),它给了我同样的错误。实际上,我希望此事务只能为此阅读。
这是有效的,我尝试恢复我做的最后一次更改,但它仍然被打破。