1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

#325 fix sync es data transaction

This commit is contained in:
mukaiu 2020-04-24 19:13:06 +08:00
parent 70ad41480d
commit 64f897ff30
3 changed files with 32 additions and 12 deletions

View File

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.persistence.RollbackException;
import java.util.List; import java.util.List;
@Aspect @Aspect
@ -70,7 +71,7 @@ public class SyncDatabaseAspect {
try { try {
// 更新时执行此代码但是新建时会报错 // 更新时执行此代码但是新建时会报错
dbDataRes.saveOrUpdate(data); dbDataRes.saveOrUpdate(data);
} catch (StaleStateException ex) { } catch (RollbackException ex) {
// 报错的情况下执行此代码 // 报错的情况下执行此代码
dbDataRes.save(data); dbDataRes.save(data);
} }

View File

@ -20,6 +20,8 @@ package com.chatopera.cc.model;
import com.chatopera.cc.basic.MainUtils; import com.chatopera.cc.basic.MainUtils;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date; import java.util.Date;
@ -37,6 +39,7 @@ public class EntCustomer extends ESBean implements java.io.Serializable {
private String id = MainUtils.getUUID(); private String id = MainUtils.getUUID();
@Field(type = FieldType.Text, fielddata = true)
private String name; private String name;
private String etype; private String etype;
private String ekind; private String ekind;
@ -89,6 +92,7 @@ public class EntCustomer extends ESBean implements java.io.Serializable {
private String processid; private String processid;
private String description; private String description;
@Field(type = FieldType.Text, fielddata = true)
private String creater; private String creater;
private String username; private String username;
private String updateuser; private String updateuser;

View File

@ -46,14 +46,17 @@ public class BaseService<T> {
*/ */
public void saveOrUpdateAll(final List<Object> ts) { public void saveOrUpdateAll(final List<Object> ts) {
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
for (final Object t : ts) { for (final Object t : ts) {
session.saveOrUpdate(t); session.saveOrUpdate(t);
} }
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); if (ex != null) {
tx.rollback();
}
throw ex;
} finally { } finally {
session.close(); session.close();
} }
@ -61,12 +64,15 @@ public class BaseService<T> {
public void saveOrUpdate(final Object t) { public void saveOrUpdate(final Object t) {
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
session.saveOrUpdate(t); session.saveOrUpdate(t);
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); if (ex != null) {
tx.rollback();
}
throw ex;
} finally { } finally {
session.close(); session.close();
} }
@ -74,12 +80,15 @@ public class BaseService<T> {
public void save(final Object t) { public void save(final Object t) {
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
session.save(t); session.save(t);
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); if (ex != null) {
tx.rollback();
}
throw ex;
} finally { } finally {
session.close(); session.close();
} }
@ -92,14 +101,17 @@ public class BaseService<T> {
*/ */
public void deleteAll(final List<Object> objects) { public void deleteAll(final List<Object> objects) {
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
for (final Object t : objects) { for (final Object t : objects) {
session.delete(session.merge(t)); session.delete(session.merge(t));
} }
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); if (ex != null) {
tx.rollback();
}
throw ex;
} finally { } finally {
session.close(); session.close();
} }
@ -107,12 +119,15 @@ public class BaseService<T> {
public void delete(final Object object) { public void delete(final Object object) {
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
session.delete(session.merge(object)); session.delete(session.merge(object));
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); if (ex != null) {
tx.rollback();
}
throw ex;
} finally { } finally {
session.close(); session.close();
} }
@ -122,8 +137,8 @@ public class BaseService<T> {
public List<T> list(final String bean) { public List<T> list(final String bean) {
List<T> dataList = null; List<T> dataList = null;
Session session = hibernateFactory.openSession(); Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try { try {
Transaction tx = session.beginTransaction();
dataList = session.createCriteria(Class.forName(bean)).list(); dataList = session.createCriteria(Class.forName(bean)).list();
tx.commit(); tx.commit();
} catch (Exception ex) { } catch (Exception ex) {