I am running hibernate with spring and have some trouble with saving my changes persistent.
My class is a litte sequence generator and shall increment the nextValue in a transaction.
But whenever the application start, the same initial values are used. The code works, the
sequence is incremented, but changes are not written to database. Ich think the changes were
not written ti database, but only to the hibernate cache.
My unit test code:
Code:
ClassPathResource res = new ClassPathResource("spring.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
SequenceDaoImpl cl = (SequenceDaoImpl) factory.getBean("fooService");
for (int idx = 0; idx < 5; idx++) {
long val = cl.nextValue("hallo");
System.out.println("nextValue=" + val );
}
Spring configuration:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="de.bytefusion.cashflow.core.sequence.SequenceDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="transactionManager" ref="transactionManager"/>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven/>
<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
And my service implementation:
Code:
...
import org.springframework.transaction.annotation.Transactional;
...
@Transactional
public class SequenceDaoImpl implements ISequence {
private HibernateTemplate hibernateTemplate;
private PlatformTransactionManager transactionManager;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
public long nextValue(String name) throws ToolsException {
long nextId = 0;
Sequence seq = (Sequence) hibernateTemplate.get(Sequence.class, name);
if ( seq == null ) {
seq = new Sequence();
seq.setName(name);
seq.setNextValue(1);
hibernateTemplate.save(name, seq);
} else {
nextId = seq.getNextValue();
seq.setNextValue(nextId+1);
hibernateTemplate.update(seq);
}
return nextId;
}
}
What's wrong?
Thx, markus