当我们在休眠中有依赖实体并尝试在 Vertica 中使用多个线程更新 table 时出现死锁
Deadlock when we we have dependent entities in hibernate and try to update table using multiple threads in Vertica
我有两个java类。 Father.java and Children.java
@Entity
@Table(name = "FATHER")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
class Father implements Cloneable
{
@Id
@Column(name = "father_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long fatherId;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "father_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<Children> children = new ArrayList<Children>();
//getter and setters and public constructors
}
@Entity
@Table(name = "Children")
class Children implements Comparable<Children>
{
@JsonIgnore
@Id
@Column(name = "children_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long child_id;
@JsonIgnore
@Column(name = "father_id")
private long fatherId;
//public constructors and getters and setters
}
public interface RelationDao{
public Father update() throws Exception;
}
@Repository("relationDao")
@EnableTransactionManagement
@Transactional
public RelationDaoImpl{
@Override
@Transactional("txManager")
public Father update(Father father)
{
father = merge(father);
//added retry logic as well also father is updated with a new child which is why merge
}
}
如果多个线程使用不同的行条目访问相同的 table(实体父)到 更新,即使记录不同,我也会收到 Deadlock X 异常。
有什么方法可以解决为什么整个 table 被锁定而不是只有一行?
即使我没有更新或添加任何代码,事务隔离级别是 SERIALIZABLE。
数据库系统是Vertica
解释到这里,如果有人来这里检查为什么Vertica不支持更新或删除的行级锁。
所以我使用synchronized
来执行线程安全的更新和删除。
我有两个java类。 Father.java and Children.java
@Entity
@Table(name = "FATHER")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
class Father implements Cloneable
{
@Id
@Column(name = "father_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long fatherId;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "father_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<Children> children = new ArrayList<Children>();
//getter and setters and public constructors
}
@Entity
@Table(name = "Children")
class Children implements Comparable<Children>
{
@JsonIgnore
@Id
@Column(name = "children_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long child_id;
@JsonIgnore
@Column(name = "father_id")
private long fatherId;
//public constructors and getters and setters
}
public interface RelationDao{
public Father update() throws Exception;
}
@Repository("relationDao")
@EnableTransactionManagement
@Transactional
public RelationDaoImpl{
@Override
@Transactional("txManager")
public Father update(Father father)
{
father = merge(father);
//added retry logic as well also father is updated with a new child which is why merge
}
}
如果多个线程使用不同的行条目访问相同的 table(实体父)到 更新,即使记录不同,我也会收到 Deadlock X 异常。
有什么方法可以解决为什么整个 table 被锁定而不是只有一行? 即使我没有更新或添加任何代码,事务隔离级别是 SERIALIZABLE。
数据库系统是Vertica
解释到这里,如果有人来这里检查为什么Vertica不支持更新或删除的行级锁。
所以我使用synchronized
来执行线程安全的更新和删除。