oneToMany bidirectional does not work when datasource has transactions set as serializable
i have a app that uses Spring,Struts, Hibernate and JPA. So i have two entities, Company and Location. Company is in oneToMany relation with Location, and location in ManyToOne to Company.
Location Entity:
@Entity<br>
@Table(name = "locations")<br>
@Access(AccessType.PROPERTY)<br>
public class Location implements Serializable, Comparable<Location> {
private Company company;
@ManyToOne
@JoinColumn(name="company_id")
public Company getCompany(){
return this.company;
}
public void setCompany(Company c){
this.company = c;
}
}
Company Entity:
@Entity
@Access(AccessType.PROPERTY)
@Table(name = "company")
public class Company implements Serializable {
private Integer id;
private String name;
private List<Location> locations;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="company")
public List<Location> getLocations(){
return this.locations;
}
public void setLocations(List<Location> l){
this.locations = l;
}
public void addLocation(Location l){
if (locations == null)
locations = new ArrayList<Location>();
if (!locations.contains(l))
locations.add(l);
if (l.getCompany()!=this)
l.setCompany(this);
}
public void removeLocation(Location l){
if (locations != null){
if (locations.contains(l))
locations.remove(l);
}
}
}
and then when i want to add a new location i have a method in locationService :
GenericService:
public abstract class GenericService {
protected Logger logger = Logger.getLogger(getClass());
@PersistenceContext(type = PersistenceContextType.EXTENDED,unitName = "MyPU")
protected EntityManager em;
public void setEntityManager(EntityManager em) {
this.em = em;
}
public EntityManager getEntityManager() {
return em;
}
}
Location Service:
@Transactional
public class LocationServiceImpl extends GenericService implements iLocationService {
@Override
public Boolean saveLocation(LocationForm lf) {
Location l = new Location();
Company c = companyService.getCompany(lf.getCompanyForm().getId());
// set all location properties here from LocationForm Obj
l.setCompany(c);
this.em.persist(l);
c.addLocation(l);
return true;
}
}
I have to specify that as a conection pool i use glassfish JDBC Connection Pool where i have enabled transactions with repetable read level. Everything is ok now, but if a switch from repetable read to serializable saveLocation method works no more.
This is the debug log when i runsaveLocation()
with serialize transaction level:
INFO: DEBUG [http-thread-pool-8080(5)] (SQLStatementLogger.java:111) -
insert
into
locations
(company_id, emailTransfer, liveTransfer, name, outbound_prefix, queue_id, smsTransfer, welcomeMessage)
values
(?, ?, ?, ?, ?, ?, ?, ?)
INFO: DEBUG [http-thread-pool-8080(5)] (SQLStatementLogger.java:111) -
select
locations0_.company_id as company9_153_1_,
locations0_.id as id1_,
locations0_.id as id146_0_,
locations0_.company_id as company9_146_0_,
locations0_.emailTransfer as emailTra2_146_0_,
locations0_.liveTransfer as liveTran3_146_0_,
locations0_.name as name146_0_,
locations0_.outbound_prefix as outbound5_146_0_,
locations0_.queue_id as queue6_146_0_,
locations0_.smsTransfer as smsTrans7_146_0_,
locations0_.welcomeMessage as we开发者_开发百科lcomeM8_146_0_
from
locations locations0_
where
locations0_.company_id=?
So then i get :
INFO: WARN [http-thread-pool-8080(5)] (JDBCExceptionReporter.java:233) - SQL Error: 1205, SQLState: 41000
INFO: ERROR [http-thread-pool-8080(5)] (JDBCExceptionReporter.java:234) - Lock wait timeout exceeded; try restarting transaction
some parts from applicationContext.xml
<bean id="txManagerVA" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emfVA" />
</bean>
<bean id="emfVA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="vsDS" />
<property name="persistenceUnitName" value="MyPU"/>
</bean>
<jee:jndi-lookup id="vsDS" jndi-name="jdbc/MyJndiDS"/>
<tx:annotation-driven transaction-manager="txManagerVA" />
It seems that after that insert the table is locked and no other operation can be performed upon it. As i said before if i change the Transaction Isolation to Repetable Read everything is ok.
Can someone explain me this behavior ?Thanks
Add cascade level
@OneToMany(mappedBy="company", cascade = CascadeType.ALL)
public List<Location> getLocations(){
return this.locations;
}
Then add locations to company where ever and save company not the locations.You are running into a deadlock otherwise.
精彩评论