springspring-orm

@Transactional in spring working for read only but not write


Previously, I used to explicitly begin and commit the transactions but now I am trying to make use of @Transactional annotation in spring. For some reason, all the read-only operations are working but persist() and merge() operations fail to work and I don't see anything on the logs. While debugging i also see that the spring has created the proxy but not sure where i am going wrong. Here is my code and spring application context configuration.

1.Jpa-context.xml

<context:annotation-config />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="PersistenceUnit" />
</bean>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

2.Framework-context.xml

<context:component-scan base-package="com.github.djuloori.whiteboard.framework" />
<context:annotation-config/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id = "m_SecurableEntityManager"  class = "com.github.djuloori.whiteboard.framework.SecurableEntityManagerImpl"/>

3.SecurableEntityManagerImpl.Java

@Service
@Transactional
public class SecurableEntityManagerImpl implements SecurableEntityManager  {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    public void save(Object var1) {
        getEntityManager().persist(var1);
    }

    public <T> TypedQuery<T> createQuery(String var1, Class<T> var2) {
        return getEntityManager().createNamedQuery(var1,var2);
    }

    public <T> T update(T var1) {
        return getEntityManager().merge(var1);
    }


    private EntityManager getEntityManager(){
        return entityManagerFactory.createEntityManager();
    }
}

I was also reading other questions related to @transactional but haven't found a proper answer. I am new to spring, It would be great if anyone can help me.


Solution

  • Autowiring EntityManager fixed the issue