PDA

View Full Version : HibernateTemplate



kristian
Sep 3rd, 2004, 01:44 PM
Hi
I'm trying to use HibernateTemplate. When using .saveOrUpdate(Object) it works, but when using .save(Object) I get an SQLException, saying that the database is in read only mode.

This is my TransactionProxyFactoryBean:

<bean id="userStore" class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="userStoreTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

Anyone got any idea? I've probably just misunderstood something.
Thanks
- Kristian

irbouho
Sep 3rd, 2004, 01:53 PM
Kristian,
Can your elaborate more, are your calling saveOrUpdate / save from the same method or from different methods? Could you also provide the log?

kristian
Sep 3rd, 2004, 02:08 PM
Hi
Ok, this is the two methods I'm trying to use:

public int addUser(
String userName, String surName, String firstName,
String telephoneNumber, String faxNumber, String cellNumber,
String emailAddress, String userURL,
UserRole userRole) {

User user = new User();
user.setUserName(userName);
user.setSurName(surName);
user.setFirstName(firstName);
user.setTelephoneNumber(telephoneNumber);
user.setFaxNumber(faxNumber);
user.setCellNumber(cellNumber);
user.setEmailAddress(emailAddress);
user.setUserURL(userURL);

return ((Integer)getHibernateTemplate().save(user)).intVa lue();
}

public void saveUser(User user)
{
getHibernateTemplate().saveOrUpdate(user);

if (log.isDebugEnabled()) {
log.debug("userID set to: " + user.getUserID());
}
}

I'm trying to use theese two methods from the unit test class, from the same unit test, and I only try to call one of them. When I call saveUser(user) it works, but when calling addUser(........) I get this:

http://folk.uio.no/krin/spring/log.txt

It was so much output, so I figured putting it in a file would be best.

Thanks
- Kristian

irbouho
Sep 3rd, 2004, 04:26 PM
Kristian,

The log result is absolutely correct:


...
Getting transaction for method 'addUser' in class [no.uio.dhis.userstore.UserStore]
...
Setting JDBC connection [org.hsqldb.jdbcConnection@1e7c5cb] read-only
...


Method no.uio.dhis.userstore.UserStore.addUser is wrapped by a readonly Transaction as stated in your transaction demarcation


<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>

If you want your method to be wrapped with a read-write transaction, either change its name to "insertUser" or add a <prop key="add*">PROPAGATION_REQUIRED</prop> to transactionAttributes.

As for method saveUser, It should fail too according to value of transactionAttributes. Could you show the log when using this method?

kristian
Sep 6th, 2004, 02:30 PM
Hi
Thanks, I've misunderstood transactionAttributes. That part is now ok =) But when I now try to save one of my mapped objects I get an SQLException saying that it can't find the table, even though I see from the debug output that it gets created:

This is the debug output/log:
http://folk.uio.no/krin/spring/spring.txt

Anyone got any idea about this?
Thanks,
Kristian