Back Forum Reply New

Hiberante transaction doesn't work

mons.dbcp.BasicDataSourcequot; destroy-method=quot;closequot;gt;
lt;property name=quot;driverClassNamequot;gt;
lt;valuegt;oracle.jdbc.driver.OracleDriverlt;/valuegt;
lt;/propertygt;
lt;property name=quot;uclquot;gt;
lt;valuegt;jdbcracle:thinlocalhost:1521/XElt;/valuegt;
lt;/propertygt;
lt;property name=quot;usernamequot; value=quot;tquot; /gt;
lt;property name=quot;passwordquot; value=quot;tquot; /gt;
lt;/beangt;

lt;!-- Database Property --gt;
lt;bean id=quot;hibernatePropertiesquot;
class=quot;org..beans.factory.config.PropertiesFactoryBeanquot;gt;
lt;property name=quot;propertiesquot;gt;
lt;propsgt;
lt;prop key=quot;hibernate.hbm2ddl.autoquot;gt;createlt;/propgt;
lt;prop key=quot;hibernate.dialectquot;gt;
org.hibernate.dialect.OracleDialect
lt;/propgt;

lt;prop key=quot;hibernate.cache.use_query_cachequot;gt;truelt;/propgt;
lt;prop key=quot;hibernate.cache.provider_classquot;gt;org.hibernate.cache.OSCacheProviderlt;/propgt;
lt;prop key=quot;hibernate.show_sqlquot;gt;truelt;/propgt;
lt;prop key=quot;hibernate.c3p0.minPoolSizequot;gt;5lt;/propgt;
lt;prop key=quot;hibernate.c3p0.maxPoolSizequot;gt;20lt;/propgt;
lt;prop key=quot;hibernate.c3p0.timeoutquot;gt;600lt;/propgt;
lt;prop key=quot;hibernate.c3p0.max_statementquot;gt;50lt;/propgt;
lt;prop key=quot;hibernate.c3p0.testConnectionOnCheckoutquot;gt;
false
lt;/propgt;
lt;/propsgt;
lt;/propertygt;
lt;/beangt;

lt;!-- Hibernate SessionFactory --gt;
lt;bean id=quot;sessionFactoryquot;
class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot;gt;
lt;property name=quot;dataSourcequot;gt;
lt;ref local=quot;dataSourcequot; /gt;
lt;/propertygt;
lt;property name=quot;hibernatePropertiesquot;gt;
lt;ref bean=quot;hibernatePropertiesquot; /gt;
lt;/propertygt;
lt;property name=quot;mappingResourcesquot;gt;
lt;listgt;
lt;valuegt;project.hbm.xmllt;/valuegt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;

In the Dao,

Code:
public void addPeople(String name){
People p = new People();
p.setTitle(name);
Ticket t = new Ticket();
List ticketList = new ArrayList();
ticketList.add(t);
p.setTicketList(ticketList);
this.getHibernateTemplate().saveOrUpdate(p);
}
In the service,

Code:
public void increasePrice() throws Exception {
//System.out.println(quot;---invoke dao, get result=quot;+mtsDao.getPeople());
mtsDao.addPeople(quot;manager4quot;);
System.out.println(quot;---throw excetpion, test transaction---quot;);
if(true)
throw new Exception(quot;test transactionquot;);
mtsDao.addPeople(quot;staff4quot;);

}
test transaction, but failed, the data is not roll-back.

Any hints ?  Did I miss anything ?

Thanks.

You've thrown an Exception which isn't rolled back by default, if you make this RuntimeException it would work.  Otherwise you need to tell the TransactionProxyFactoryBean to rollback for Exception.
In this example, note that the value for the insert* mapping contains a rollback rule. Adding -MyCheckedException here specifies that if the method throws MyCheckedException or any subclasses, the transaction will automatically be rolled back. Multiple rollback rules can be specified here, comma-separated. A - prefix forces rollback; a + prefix specifies commit. (This allows commit even on unchecked exceptions, if you really know what you're doing!)

sp...n.html#d0e5690

Hello,

Spring only rollbacks an exception if there is any runtime exception thrown (or an error). In your case this is not the reason, that's the reasin why the Transaction is commited.

Have a look in the reference Manual here

regards
agim


Originally Posted by LysergSpring only rollbacks an exception if there is any runtime exception thrown (or an error). In your case this is not the reason, that's the reasin why the Transaction is commited.

Thanks. Even I force it throw RuntimeException, still NOT work. Maybe something else.

Do I need to set up hibernate transaction factory ? or some other things ?

Thanks.

Can you post your stacktrace and the test code you are using?


Originally Posted by karldmooreCan you post your stacktrace and the test code you are using?

Kardmoore,

Thanks for your quick reply. Here is my log attachment.

Thanks.

I think I found the reason.

The reason is I wire wrong service to controller. I should wire quot;MtsServicequot;, instead of quot;MtsServiceTargetquot;.

Much appreciated your guys reply. This forum always gives me lot of help.

Thanks
¥
Back Forum Reply New