Issue
I am trying to save/persist multiple objects from a collection to a DB but i keep getting error. I have debugged and found out that this issue come when i try to save product. This exception comes on my ec2 instance server on my local workspace/machine.
ERORR:
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: Transaction already active
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy137.findOne(Unknown Source)
Persistence.xml :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- value="create" to build a new database on each run;
value="update" to modify an existing database;
value="create-drop" means the same as "create" but also drops tables when Hibernate closes;
value="validate" makes no changes to the database -->
<property name="hibernate.physical_naming_strategy" value="com.canland.business.annotations.PhysicalNamingStrategyImpl"/>
<!-- <property name="hibernate.ejb.naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl" /> -->
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl" />
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.use_sql_comments" value="false"/>
<property name="hibernate.id.new_generator_mappings" value="false"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<!-- <property name="hibernate.event.merge.entity_copy_observer" value="allow"/> -->
<property name="org.hibernate.envers.audit_table_suffix" value="_aud"/>
<property name="org.hibernate.envers.store_data_at_delete" value="true"/>
<!-- <property name="org.hibernate.envers.revision_field_name" value="rev"/>
<property name="org.hibernate.envers.revision_type_field_name" value="revtype"/>
<property name="org.hibernate.envers.audit_strategy" value="org.hibernate.envers.strategy.ValidityAuditStrategy"/>
<property name="org.hibernate.envers.audit_strategy_validity_end_rev_field_name" value="AUDIT_REVISION_END"/>
<property name="org.hibernate.envers.audit_strategy_validity_store_revend_timestamp" value="true"/>
<property name="org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name" value="AUDIT_REVISION_END_TS"/> -->
<!-- Uncomment the following two properties for JBoss only -->
<!-- <property name="hibernate.validator.apply_to_ddl" value="false"/>
<property name="hibernate.validator.autoregister_listeners" value="false"/>-->
<!-- <property name="hibernate.ejb.event.post-insert"
value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-update"
value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-delete"
value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-update"
value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-remove"
value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-collection-recreate"
value="org.hibernate.envers.event.AuditEventListener" /> -->
</properties>
</persistence-unit>
</persistence>
Code to Save a collection :
@Autowired
private Prodservice prodservice;
public saveValue(List<Product> products){
for(Product p : products){
p.setSomething("");
prodservice.saveIteratedProduct(p);
}
}
ProdService.class: method
@Autowired
private ProductRepository productRepository
saveIteratedProduct(Product p){
Product product = null;
try {
product = productRepository.save(p);
} catch (Exception e) {
e.printStackTrace();
}
return product;
}
I am using simple JpaRespostitory to save the data in my repositories.
Solution
I have fixed the issue, the issue was with the hibernate itself, i am using 5.2.1 and this issue was fixed in 5.3
I updated my hibernate maven dependency to 5.3.0 and the issue
See here for the fix
Answered By - Sidharth