I did for a long time. but I dit not find the problem.
the data can't rollback on exception.
the codes use two sessionFactory with different dataSource.
but I've commented session.save() and session.delete from
sourceSessionFactory for debug. so , it doesn't need JTA
Now , there is only a session works from xfileSessionFactory
the codes are:
xfileSession.save(xource);
..............
xfileSession.save(image);
I thrown a exception on the end, the data still saved to database not rollback.
what's the problem?
Code:
Code:
public class ImportWS01001FromTempImpl implements XourceImporter {
private SessionFactory sourceSessionFactory;
private SessionFactory xfileSessionFactory;
public void importXource(Object data) throws DataAccessException {
ImportWsDjxxView model = (ImportWsDjxxView)data;
for (int i = 0; i < model.getWspzxh().length; i++) {
Session session = null;
Session xfileSession = null;
try {
session = SessionFactoryUtils.getSession(sourceSessionFactory, true); //sourceSessionFactory.openSession();
xfileSession = SessionFactoryUtils.getSession(xfileSessionFactory, true); //xfileSessionFactory.openSession();
Query query = session.createQuery("from KydaWsDjxxTmp wsDjxx where (wsDjxx.nsrsbh=:nsrsbh) and (wsDjxx.wspzxh=:wspzxh)");
query.setString("nsrsbh", model.getNsrsbh()[i]);
query.setString("wspzxh", model.getWspzxh()[i]);
Iterator it = query.iterate();
if (it.hasNext()) {
KydaWsDjxxTmp wsDjxx = (KydaWsDjxxTmp)it.next();
StringBuffer uri = new StringBuffer();
uri.append("model.wspzxh=");
uri.append(wsDjxx.getWspzxh());
uri.append("&");
uri.append("model.nsrsbh=");
uri.append(wsDjxx.getNsrsbh());
uri.append("&");
uri.append("model.wszldm=");
uri.append(wsDjxx.getWszlDm());
Xtype xtype = new Xtype();
xtype.setSymbol("WS" + wsDjxx.getWszlDm());
Xource xource = new Xource();
xource.setXtype(xtype);
xource.setUri(uri.toString());
xource.setTitle(wsDjxx.getWszlDm());
xource.setCreateTime(wsDjxx.getSlxhrq());
xource.setXourceOwner(wsDjxx.getNsrsbh());
xource.setXourceGroup(wsDjxx.getSwjgDm());
xource.setCollectTime(new Date());
xfileSession.save(xource);
KydaWsDjxxGd wsDjxx2 = new KydaWsDjxxGd();
BeanUtils.copyProperties(wsDjxx2, wsDjxx);
// session.save(wsDjxx2);
// session.delete(wsDjxx);
StringBuffer hql = new StringBuffer();
hql.append(" from KydaFszl fszl where (fszl.wspzxh=:wspzxh) and (fszl.nsrsbh=:nsrsbh) ");
hql.append(" order by fszl.qcfwDmWsfxzl.wsfszlDm, fszl.pageNo ");
Query fszlQuery = session.createQuery(hql.toString());
fszlQuery.setString("nsrsbh", model.getNsrsbh()[i]);
fszlQuery.setString("wspzxh", model.getWspzxh()[i]);
it = fszlQuery.iterate();
while (it.hasNext()) {
KydaFszl row = (KydaFszl)it.next();
Xtype imageXtype = new Xtype();
imageXtype.setSymbol("IMAGE");
Xource image = new Xource();
image.setXtype(imageXtype);
uri = new StringBuffer();
uri.append("fileName=");
uri.append(row.getFileName());
image.setUri(uri.toString());
image.setCreateTime(row.getUploadtime());
image.setTitle(row.getQcfwDmWsfxzl().getWsfszlMc() + " page:" + row.getPageNo());
image.setNote(row.getQcfwDmWsfxzl().getWsfszlMc());
image.setXourceOwner(row.getNsrsbh());
image.setCollectTime(new Date());
image.setXourceGroup(xource.getXourceGroup());
xfileSession.save(image);
XourceRelation relation = new XourceRelation();
XourceRelationId id = new XourceRelationId();
id.setXourceMain(xource);
id.setXourceFriend(image);
relation.setId(id);
relation.setTitle(image.getTitle());
xfileSession.save(relation);
throw new HibernateException("xxxxxxxx");
}
xfileSession.update(xource);
session.flush();
xfileSession.flush();
}
} catch(HibernateException he) {
throw SessionFactoryUtils.convertHibernateAccessException(he);
} catch(IllegalAccessException iae) {
iae.printStackTrace();
} catch(InvocationTargetException ite) {
ite.printStackTrace();
} finally {
SessionFactoryUtils.closeSessionIfNecessary(session, sourceSessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(xfileSession, xfileSessionFactory);
}
}
}
}
Code:
Code:
<bean id="xfileSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
</bean>
<bean id="xfileHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="xfileSessionFactory"/>
</property>
</bean>
<bean id="xfileTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="xfileSessionFactory"/>
</property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="false">
<property name="transactionManager"><ref bean="xfileTransactionManager"/></property>
<property name="target">
<ref bean="importWS01001FromTempImpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="import*">PROPAGATION_REQUIRED</prop>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="importWS01001FromTempImpl" class="com.fjky.xfile.enchance.impl.ImportWS01001FromTempImpl">
<property name="xfileSessionFactory">
<ref bean="xfileSessionFactory"/>
</property>
<property name="sourceSessionFactory">
<ref bean="ctaisOldSessionFactory"/>
</property>
</bean>