技术背景:项目采用EJB3.1,数据库使用Orac11G、中间件使用WAS8.5 ,系统采用AIX
项目背景:用户将XML文件上传到服务器,服务器解析并保存,保存完毕后,对于数据进行校验
问题描述:当高并发上来后,总有少量用户的的数据在数据校验失败。
失败原因:校验时查询数据库中的数据不全,但数据库中数据是全的。例如:数据库中有5000条这个用户的数据,在校验时,只发现1000条。
代码:
保存:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void addBatchImm(List<Pisaodt3010> pisaList) throws SQLException
{
if (pisaList == null || pisaList.size() == 0)
{
return;
}
Connection conn = null;
String sql = "insert /*+append*/ into Pisaodt3010(id,altime,subage,periods,freq,part,indexno,dimno,orgno,areatype,areacode,BUSFLAG,varvalue,varvalue2) values(SEQ_COLLDA_ID.nextval,sysdate,?,?,?,?,?,?,?,?,?,'1',?,?)";
try
{
conn = (Connection) ((OpenJPAEntityManager) getEm().getDelegate()).getConnection()
PreparedStatement ps = conn.prepareStatement(sql);
for (Pisaodt3010 pisa : pisaList)
{
ps.setString(1, pisa.getSubage());
ps.setString(2, pisa.getPeriods());
ps.setString(3, pisa.getFreq());
ps.setString(4, pisa.getPart());
ps.setString(5, pisa.getIndexno());
ps.setString(6, pisa.getDimno());
ps.setString(7, pisa.getOrgno());
ps.setString(8, pisa.getAreatype());
ps.setString(9, pisa.getAreacode());
ps.setString(10, pisa.getVarvalue());
ps.setString(11, pisa.getVarvalue2());
ps.addBatch();
}
ps.executeBatch();
ps.close();
conn.close();
} catch (SQLException e)
{
logger.error("批量保存数据异常!", e);
throw e;
}
this.flush();
this.clear();
}
查询:
List countList = pisaodt3010Dao.queryNativeSQLList("select count(*) from Pisaodt3010 where part='"+part+"' and subage=?1", new String[]{subage});
logger.info("机构报送总条数["+countList.get(0)+"]");
解决方式:
entityManager.getEntityManagerFactory().getCache().evictAll(); 清空二级缓存;