【发布时间】:2019-07-04 06:57:09
【问题描述】:
我正在使用 springboot 应用程序并连接到 Azure Mysql。 在应用程序内部的方法调用中,编写了一个围绕 Spring 的方面,它可以调用 Azure MySql db。 以下是从切面和方法执行的查询序列
@Autowired
EntityManager entityManager
- 来自方面:
insert into <table name > values ();使用以下代码来执行此查询
EntityManager newEntityManager = entityManager.getEntityManagerFactory().createEntityManager();
String nativeSql = "insert into table1 values('1','abc;)";
newEntityManager.getTransaction().begin();
try{
newEntityManager.createNativeQuery(nativeSQL).executeUpdate();
entityManager.getTransaction().commit();
} catch (RuntimeException e) {
newEntityManager.getTransaction().rollback();
}finally{
newEntityManager.close();
}
- 使用带有注释的 JPA 在数据库上完成读取调用
@Transactional(readOnly=true)
- 下一个被执行的片段
EntityManager newEntityManager = entityManager.getEntityManagerFactory().createEntityManager();
String nativeSql = "update table2 set status='dead' where name='abc'";
newEntityManager.getTransaction().begin();
try{
newEntityManager.createNativeQuery(nativeSQL).executeUpdate(); //error occurs at this line
entityManager.getTransaction().commit();
} catch (RuntimeException e) {
newEntityManager.getTransaction().rollback();
}finally{
newEntityManager.close();
}
以下是完整的错误
2019-02-10 23:18:00.959 错误 [bootstrap,c577f32a3b673745,c577f32a3b673745,false] 10628 --- [nio-8106-exec-2] ohengine.jdbc.spi.SqlExceptionHelper:无法在只读事务。
但是当应用程序连接到本地 mysql(在我的例子中是 MariaDb)时,相同的代码可以正常工作。 即使与 Azure MSSQL 连接,此代码也能正常工作。 但是连接Azure MySQL时会出现错误。
【问题讨论】:
-
检查您的连接详细信息。您的连接应该在主服务器上
-
我遇到了完全相同的问题,但我没有找到要检查的内容。我试过
show variables WHERE Variable_name like 'tx_%'但这清楚地表明tx_read_only是OFF。它适用于类似的 MySQL 服务器,到目前为止我迷路了。当我通过我的 IDE 使用相同的 JDBC URL、用户和密码连接到数据库时,我可以执行更改。即使应用程序之前成功调用了清理过程,但随后它拒绝在类似于上一个代码示例的块中执行一次 INSERT。不知道。
标签: mysql hibernate azure spring-boot spring-data-jpa