I'm trying to implement a simple DAO. I have a Dao:
@Repository("iUserDao")
@Transactional(readOnly = true)
public class UserDao implements IUserDao {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public User getById(int id) {
return entityManager.find(User.class, id);
}
@Override
public boolean save(User user) {
entityManager.persist(user);
entityManager.flush();
return true;
}
@Override
public boolean update(User user) {
entityManager.merge(user);
entityManager.flush();
return true;
}
@Override
public boolean delete(User user) {
user = entityManager.getReference(User.class, user.getId());
if (user == null)
return false;
entityManager.remove(user);
entityManager.flush();
return true;
}
And an entity:
@Entity
@Table(name = "users")
public class User {
private int id;
private Date creationDate;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public User() {
}
public User(Date creationDate) {
this.creationDate = creationDate;
}
public void setId(int id) {
this.id = id;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
}
Here's appContext.xml: `
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter"
p:persistenceUnitName="test">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL" p:showSql="true"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven/>`
Unless I call flush()
after persist()
or merge()
the insert in not executed.
Why is that?
If I remove @Transactional
then I get error on "no transaction is in progress" on flush, but if remove flush nothing inserted to the DB.
It works this way because you marked transaction as read only with @Transactional(readOnly = true)
.
As you can see, it doesn't make your transactions actually read-only since you still can persist changes by calling flush()
manually. However, it disables automatic flush at the end of transaction, so that changes are not persisted without manual flush.
You need either remove readOnly
from class-level annotation, or override it on non-read-only methods with method-level annotations:
@Override
@Transactional(readOnly = false)
public boolean save(User user) { ... }
Also note that transaction demarcation is usually applied to service layer methods, not to DAO methods. In particular, when writing DAO methods you actually don't know which transactions should be read-only, and which are not. This information is available only when designing service layer, as you can see in this example:
public class UserService {
@Autowired UserDAO dao;
@Transactional(readOnly = true)
public User getUserById(int id) {
return dao.getById(id); // getById() can participate in effectively read-only transaction
}
@Transactional
public void changeUserName(int id, String newName) {
User u = dao.getById(id); // Or not
u.setName(newName); // Change will be flushed at the end of transaction
}
}
Try setting the transactional propagation to required, and remove all flush()
:-
@Repository("iUserDao")
@Transactional(propagation=Propagation.REQUIRED)
public class UserDao implements IUserDao {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With