Commit 64f897ff authored by mukaiu's avatar mukaiu
Browse files

#325 fix sync es data transaction

No related merge requests found
Showing with 32 additions and 12 deletions
+32 -12
......@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.persistence.RollbackException;
import java.util.List;
@Aspect
......@@ -70,7 +71,7 @@ public class SyncDatabaseAspect {
try {
// 更新时,执行此代码,但是新建时会报错
dbDataRes.saveOrUpdate(data);
} catch (StaleStateException ex) {
} catch (RollbackException ex) {
// 报错的情况下,执行此代码
dbDataRes.save(data);
}
......
......@@ -20,6 +20,8 @@ package com.chatopera.cc.model;
import com.chatopera.cc.basic.MainUtils;
import org.hibernate.annotations.GenericGenerator;
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 java.util.Date;
......@@ -37,6 +39,7 @@ public class EntCustomer extends ESBean implements java.io.Serializable {
private String id = MainUtils.getUUID();
@Field(type = FieldType.Text, fielddata = true)
private String name;
private String etype;
private String ekind;
......@@ -89,6 +92,7 @@ public class EntCustomer extends ESBean implements java.io.Serializable {
private String processid;
private String description;
@Field(type = FieldType.Text, fielddata = true)
private String creater;
private String username;
private String updateuser;
......
......@@ -46,14 +46,17 @@ public class BaseService<T> {
*/
public void saveOrUpdateAll(final List<Object> ts) {
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
for (final Object t : ts) {
session.saveOrUpdate(t);
}
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (ex != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
......@@ -61,12 +64,15 @@ public class BaseService<T> {
public void saveOrUpdate(final Object t) {
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
session.saveOrUpdate(t);
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (ex != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
......@@ -74,12 +80,15 @@ public class BaseService<T> {
public void save(final Object t) {
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (ex != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
......@@ -92,14 +101,17 @@ public class BaseService<T> {
*/
public void deleteAll(final List<Object> objects) {
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
for (final Object t : objects) {
session.delete(session.merge(t));
}
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (ex != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
......@@ -107,12 +119,15 @@ public class BaseService<T> {
public void delete(final Object object) {
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
session.delete(session.merge(object));
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (ex != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
......@@ -122,8 +137,8 @@ public class BaseService<T> {
public List<T> list(final String bean) {
List<T> dataList = null;
Session session = hibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Transaction tx = session.beginTransaction();
dataList = session.createCriteria(Class.forName(bean)).list();
tx.commit();
} catch (Exception ex) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment