i have the same problem, my Transactions not write into DB. I think this Spring problem regard is not a bug of framework, but is a problem of configuration file (applicationContext.xml for understand) so I put my configuration file:
...
<!-- this is a bridge for entityManager and PersistenceUnit -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fb-persistence" />
</bean>
<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean name="pluginDaoImpl" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" />
</beans>
a interface is:
public interface PluginDao {
public Plugin load (int id);
public void save(Plugin plg);
}
a implementation of interface is:
public class PluginDaoImpl implements PluginDao {
@PersistenceContext (unitName="fb-persistence")
private EntityManager em;
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Transactional
public Plugin load(int id) {
return em.find(Plugin.class, id);
}
@Transactional(readOnly=false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void save(Plugin plg) {
em.persist(plg);
//em.flush();
}
}
and dulcis in fundo, my PluginTest (for dirty-testing) is:
public class PluginTest{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
PluginDao dao = (PluginDao) applicationContext.getBean("pluginDaoImpl");
Plugin plugin1 = new Plugin();
//inserisco un nuovo plugin
//plugin.setId(14);
plugin1.setMethod("prova");
plugin1.setDescrizione("questa Bean Class!");
dao.save(plugin1);
Plugin plugin = new Plugin();
//carico un plugin per id
plugin = dao.load(9);
System.out.println("id: " + plugin.getId() +
" Descrizione: " + plugin.getDescrizione() +
" Method: " + plugin.getMethod());
}
If I now add the method PluginDaoImpl.save (), em.flush line-code I get this error:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress *
*the cause is not specificated.
You are missing
<tx:annotation-driven transaction-manager="myTransactionManager"/>in your context.xml
(See Using
@Transactional)Add the tx namespace like this: