Reputation: 890
I've gone through a lot of similar question on SO, generally the answer is to just add @Transactional
to the class(es) and method(s). But this is not working for me, thus I assume I'm doing something else wrong?
MyRepository:
@Repository
public class MyRepository extends AbstractRepository<MyEntity> implements org.springframework.data.repository.Repository<MyEntity, Long> {
@PersistenceContext
private EntityManager em;
public MyRepository() {
super(MyEnitiy.class);
}
@Override
public EntityManager getEntityManager() {
return em;
}
@SuppressWarnings("unchecked")
@Transactional
public List<MyEnitiy> getAllFor(Integer id) {
return getEntityManager().createQuery("SELECT k FROM MyEntity k WHERE k.otherid = :otherid ORDER BY k.something", MyEntity.class)
.setParameter("otherid", id).getResultList();
}
}
AbstractRepository:
@Transactional(readOnly = true)
public abstract class AbstractRepository<T> {
...
@Modifying
@Transactional
public void edit(T entity) {
getEntityManager().merge(entity);
}
...
}
My Sping web controller (Snippet):
@Controller
public class MyController {
@Autowired
private MyRepository myRepository;
@RequestMapping(value = {"/here/there"}, method = RequestMethod.GET)
@ResponseBody
@Transactional
public String hereAndTherePage(@RequestParam(value = "id", required = true) Integer id,
HttpServletRequest request, HttpSession session) {
List<MyEntity> myEntities = myRepository.getAllFor(id);
for(MyEntity myEntity : myEntities) {
...
myEntity.setSomeValue(myEntity.getSomeValue() + 1);
...
myRepository.edit(myEntity);
Update:
spring-database.xml (the use of a pool was an attempt to try and fix a Tomcat memory leak):
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="poolProperties" class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="jdbc:mysql://localhost:3306/bfwinkel?noAccessToProcedureBodies=true"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testOnBorrow" value="true"/>
<property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="10"/>
<property name="username" value="d[-.-]b"/>
<property name="password" value="~!~"/>
</bean>
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="poolProperties" ref="poolProperties"/>
</bean>
The exception is thrown when getEntityManager().merge(entity);
is called:
javax.persistence.TransactionRequiredException: No transactional EntityManager available
Upvotes: 0
Views: 710
Reputation: 272
Add the below lines to your spring-database.xml
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
Upvotes: 1
Reputation: 441
This error means that, they are no available persistence context available when you try to merge your entity.
You have Three options to fix this issue by priority.
1- Have you added @EnableTransactionManagement to your configuration ?
2- If you have a persistence.xml file, you should add a unitname attribute to @PersistenceContext.
3- Use Extended Persistence context instead of Transaction-scoped Persistence context.
I hope my answer will help you.
Good luck
Upvotes: 0