我读过大约4级隔离:
Isolation Level Dirty Read Nonrepeatable Read Phantom Read
READ UNCOMMITTED Permitted Permitted Permitted
READ COMMITTED -- Permitted Permitted
REPEATABLE READ -- -- Permitted
SERIALIZABLE -- -- --
Run Code Online (Sandbox Code Playgroud)
我想了解每个事务隔离对表的锁定
READ UNCOMMITTED - no lock on table
READ COMMITTED - lock on committed data
REPEATABLE READ - lock on block of sql(which is selected by using select query)
SERIALIZABLE - lock on full table(on which Select query is fired)
Run Code Online (Sandbox Code Playgroud)
下面是事务隔离中可能发生的三种现象
Dirty Read - no lock
Nonrepeatable Read - 没有脏读作为锁定提交数据
Phantom …
更新数据库时我应该更喜欢什么?两种方法的优缺点是什么?何时使用其中一种方法?
public void disemployEmployee(Integer employeeId, Date endDate) {
Employee employee = (Employee)em.find("Employee", employeeId);
employee.getPeriod().setEndDate(endDate);
em.flush();
}
public void disemployEmployee(Integer employeeId, Date endDate) {
Employee employee = (Employee)em.find("Employee", employeeId);
em.getTransaction().begin();
employee.getPeriod().setEndDate(endDate);
em.getTransaction().commit();
}
Run Code Online (Sandbox Code Playgroud) MOTHER感谢JPQL查询,我试图删除大量的行.
所述Mother类的定义如下:
@Entity
@Table(name = "MOTHER")
public class Mother implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "mother",
orphanRemoval = true)
private List<Child> children;
}
@Entity
@Table(name = "CHILD")
public class Child implements Serializable {
@ManyToOne
@JoinColumn(name = "MOTHER_ID")
private Mother mother;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,Mother该类具有"子",并在执行以下查询时:
String deleteQuery = "DELETE FROM MOTHER WHERE some_condition";
entityManager.createQuery(deleteQuery).executeUpdate();
Run Code Online (Sandbox Code Playgroud)
抛出异常:
ERROR - ORA-02292: integrity constraint <constraint name> violated -
child record found
Run Code Online (Sandbox Code Playgroud)
当然,我可以首先选择我要删除的所有对象,然后在迭代它之前将它们检索到列表中以删除所有检索到的对象,但是这样的解决方案的性能会很糟糕!
那么有没有办法利用前面的映射来有效地删除所有Mother对象和Child与它们相关的所有对象,而无需首先为所有子项编写查询?
我的问题是为什么冲洗不起作用:
public void ejbService(){
Customer c = em.find(Customer.class,1);
c.setName("newName");
em.flush();
//at this point when I query mysql table I can not see "newName"
thread.sleep(10000);
c.setName("anotherName");
}
Run Code Online (Sandbox Code Playgroud)
完成方法后,我在db中看到"anotherName",我也用em.find(Customer.class,1,Lock.None)检查它; 但仍然不行
RGDS
我正在尝试使用 hibernate (Spring JPA) 实现以下本机查询逻辑。但是,如果其中一条记录无法持久保存,则 save(Iterable) 会引发异常并回滚整个事务。有什么方法可以捕获记录错误并继续插入其他记录。
\n\n例如:-
\n\n原生 SQL 查询
\n\nset autocommit=false\ndelete from EMPLOYEE;\ninsert into EMPLOYEE(id, name, sal) values(2, \xe2\x80\x98Roy\xe2\x80\x99, \xe2\x80\x98rt\xe2\x80\x99); \xe2\x80\x94-stmt1\ninsert into EMPLOYEE(id, name, sal) values(2, \xe2\x80\x98Joe\xe2\x80\x99, 3000);\ncommit;\nRun Code Online (Sandbox Code Playgroud)\n\n注意:Sal 列在 EMPLOYEE 表中是数字。即使 stmt1 失败,执行仍会继续。
\n\nHibernate(CrudRepository)
\n\n@Autowired\nCrudRepository employeeRepository;\n\n@Transactional\npublic void saveToDB(List dataList) {\n employeeRepository.deleteAll();\n employeeRepository.save(dataList);\n}\nRun Code Online (Sandbox Code Playgroud)\n