I want to carry out a Multi transaction by one Session.
My code is as follows.
Then, I am waiting for a reply.
Thank you.
Code:
public void csvUpload(List admArtistCSVInfoList) throws Exception {
Session session=null;
try{
/* START SESSION */
session = SessionManager.currentSession();
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
AdmArtistCSVInfo admArtistCSVInfo = null;
Long labelCd = null;
RelLabelArtistPk relLabelArtistPk = null;
Artist artist = null;
for(int i = 0; i < admArtistCSVInfoList.size(); i++){
//GET
admArtistCSVInfo = (AdmArtistCSVInfo) admArtistCSVInfoList.get(i);
labelCd = admArtistCSVInfo.getLabelCd();
relLabelArtistPk = admArtistCSVInfo.getRelLabelArtistPk();
artist = admArtistCSVInfo.getArtist();
String artistCd = artist.getArtistCd();
Long labelUnitCd = relLabelArtistPk.getLabelUnitCd();
Long artistRoleCd = relLabelArtistPk.getArtistRoleCd();
Artist artistOld = null;
Artist artistTmp = new Artist();
Artist artistNew = null;
Long artistId = null;
String queryString =
"SELECT "
+ " {artist.*} "
+ "FROM "
+ " artist as {artist} "
+ "WHERE "
+ " {artist}.artist_cd = :artistCd ";
List temp = session
.createSQLQuery(queryString, "artist", Artist.class)
.setParameter("artistCd",artistCd)
.list();
if(temp.size() == 0){ //INSERT
artist.setInsertDate(currentDate);
artist.setUpdateDate(currentDate);
//INSERT
session.save(artist);
artistNew = (Artist)session.load(Artist.class, artist.getArtistId());
}else{ //UPDATE
artistOld = (Artist)temp.get(0);
PropertyUtils.copyProperties(artistTmp, artistOld);
artistTmp.setArtistCd(artist.getArtistCd());
artistTmp.setArtistName(artist.getArtistName());
artistTmp.setArtistNameKana(artist.getArtistNameKana());
artistTmp.setArtistNameKanaNayose(artist.getArtistNameKanaNayose());
artistTmp.setBirthDay(artist.getBirthDay());
artistTmp.setSex(artist.getSex());
artistTmp.setProfile(artist.getProfile());
artistTmp.setProfilePhotoUrl(artist.getProfilePhotoUrl());
artistTmp.setMailAddress(artist.getMailAddress());
artistTmp.setFormationDate(artist.getFormationDate());
artistTmp.setLimitDate(artist.getLimitDate());
artistTmp.setUpdateDate(currentDate);
//UPDATE
session.saveOrUpdate(artistTmp);
artistNew = (Artist)session.load(Artist.class, artistTmp.getArtistId());
}
artistId = artistNew.getArtistId();
RelLabelArtist relLabelArtist = new RelLabelArtist();
if((labelCd != null) && (labelUnitCd != null)){
queryString =
"SELECT "
+ " {labelUnit.*} "
+ "FROM "
+ " label_unit as {labelUnit} "
+ " LEFT JOIN label as label ON (label.label_cd = {labelUnit}.label_cd and label.label_cd = :labelCd) "
+ "WHERE "
+ " {labelUnit}.label_unit_cd = :labelUnitCd ";
temp = session
.createSQLQuery(queryString, "labelUnit", LabelUnit.class)
.setParameter("labelCd",labelCd)
.setParameter("labelUnitCd",labelUnitCd)
.list();
if(temp.size() == 0){
//error
}else{
queryString =
"SELECT "
+ " {relLabelArtist.*} "
+ "FROM "
+ " rel_label_artist as {relLabelArtist} "
+ "WHERE "
+ " {relLabelArtist}.label_unit_cd = :labelUnitCd "
+ "AND "
+ " {relLabelArtist}.artist_id = :artistId "
+ "AND "
+ " {relLabelArtist}.artist_role_cd = :artistRoleCd ";
temp = session
.createSQLQuery(queryString, "relLabelArtist", RelLabelArtist.class)
.setParameter("labelUnitCd",labelUnitCd)
.setParameter("artistId",artistId)
.setParameter("artistRoleCd",artistRoleCd)
.list();
if(temp.size() == 0){
relLabelArtistPk.setArtistId(artistId);
relLabelArtist.setRelLabelArtistPk(relLabelArtistPk);
relLabelArtist.setInsertDate(currentDate);
relLabelArtist.setUpdateDate(currentDate);
//INSERT
session.save(relLabelArtist);
}
}
}
}//end of for
/* SESSION FLUSH */
session.flush();
} catch (Exception he) {
System.out.println("★Exception");
he.printStackTrace();
throw new SystemException(he);
} finally {
try {
SessionManager.closeSession();
} catch (Exception he) {
System.out.println("★Exception2");
he.printStackTrace();
}
}
}
Debug level Hibernate log excerpt:
10:14:00,484 INFO [STDOUT] Hibernate: SELECT artist.artist_id as artist_id0_, artist.artist_cd as artist_cd0_, artist.artist_name as artist_n3_0_, artist.parent_id as parent_id0_, artist.artist_name_kana as artist_n5_0_, artist.artist_name_kana_nayose as artist_n6_0_, artist.artist_eng_name as artist_e7_0_, artist.artist_last_name as artist_l8_0_, artist.birth_day as birth_day0_, artist.death_day as death_day0_, artist.native_place as native_11_0_, artist.sex as sex0_, artist.profile as profile0_, artist.profile_photo_url as profile14_0_, artist.special_notes as special15_0_, artist.formation_date as formati16_0_, artist.limit_date as limit_date0_, artist.mail_address as mail_ad18_0_, artist.insert_date as insert_19_0_, artist.update_date as update_20_0_, artist.delete_date as delete_21_0_ FROM artist as artist WHERE artist.artist_cd = ?
10:14:00,593 INFO [STDOUT] ★Exception
10:14:00,609 ERROR [STDERR] net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 42, of class: com.kmp.dao.domain.Artist
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1642)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1414)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1440)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1364)
at com.kmp.dao.manager.ArtistMgr.csvUpload(ArtistMgr.java:466)
at com.kmp.ejb.artist.ArtistBean.csvUpload(ArtistBean.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:683)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:331)
at org.jboss.ejb.Container.invoke(Container.java:700)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:101)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:90)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy108.csvUpload(Unknown Source)
at com.kmp.web.actions.AdmArtistCSVUploadAction.executeAction(AdmArtistCSVUploadAction.java:310)
at com.framework.web.actions.MainAction.execute(MainAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at com.framework.web.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:220)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.tc4.statistics.ContainerStatsValve.invoke(ContainerStatsValve.java:76)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:65)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:197)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)
|