我有一个使用JPA EclipseLInk 2.5.2的应用程序,它每分钟都会联系一个mysql来做事情。必须要做的第一件事就是说它对DB来说是活生生的。为此,我使用每分钟运行一个busioness对象的ScheuledExecutorService。此对象每次打开一个关闭的DAO。
在我的开发环境(我的电脑)上,默认配置了Wamp64位和mysql,一切正常。当我安装一个preprod服务器(CentOS 7,mysql Ver 14.14 Distrib 5.7.16)时,第一个循环没问题,但是当尝试用DAO执行sql查询时,下一个循环失败并显示此消息:
[EL Warning]: 2016-12-13 09:35:01.936--UnitOfWork(1778154389)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 57 316 milliseconds ago. The last packet sent successfully to the server was 4 milliseconds ago.
Error Code: 0
Call: SELECT id, lastDateAliveChargement, lastDateAliveDechargement FROM LastAlive
Query: ReportQuery(referenceClass=LastAlive sql="SELECT id, lastDateAliveChargement, lastDateAliveDechargement FROM LastAlive")
在preprod上,我首先使用了与mariaDB jdbc驱动程序一起使用的mariaDB。然后我用mysql jdbc驱动程序切换到mysql,我仍然遇到了问题。当我在mysql上列出挂起的连接时,它们仍会以命令Sleep 出现,直到wait_timeout到期为止。 我尝试使用这些设置配置mysql(my.cnf)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
max_connections=100
port=3306
wait_timeout=3600
在我的电脑上(wamp,so windows): [MySQL的] no-auto-rehash //无法在服务器上启动此参数 的[mysqld] port = 3306
以下是JPA初始化的参数:
persistenceMapInitiales.put("javax.persistence.jdbc.driver","com.mysql.cj.jdbc.Driver");
persistenceMapInitiales.put("javax.persistence.jdbc.url","jdbc:mysql://MY_REMOTE_IP/myDB?characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true&useSSL=false");
persistenceMapInitiales.put("javax.persistence.jdbc.user","toto");
persistenceMapInitiales.put("javax.persistence.jdbc.password","toto");
persistenceMapInitiales.put("eclipselink.ddl-generation","create-or-extend-tables");
persistenceMapInitiales.put("eclipselink.create-ddl-jdbc-file-name","createDDL_ddlGeneration.jdbc");
persistenceMapInitiales.put("eclipselink.drop-ddl-jdbc-file-name","dropDDL_ddlGeneration.jdbc");
persistenceMapInitiales.put("eclipselink.ddl.default-table-suffix","engine=InnoDB");
persistenceMapInitiales.put("eclipselink.ddl-generation.output-mode","both");
persistenceMapInitiales.put("eclipselink.connection-pool.default.initial","1" );
persistenceMapInitiales.put("eclipselink.connection-pool.default.min","64" );
persistenceMapInitiales.put("eclipselink.connection-pool.default.max","64" );
scheduledExectorService的声明:
public static void main(String[] args)
{
final Integer CRON_PERIOD = 1; // every minute
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new MyWork(), 0, CRON_PERIOD, TimeUnit.MINUTES);
}
商务舱
public class MyWork() implements Runnable{
private LastAliveDao lastAliveDao;
//...
public void run()
{
sayIsAlive();
//...
}
private void sayIsAlive()
{
logger.info("upadet isAlive");
try{
lastAliveDao = new JpaLastAliveDao();
lastAliveDao.sayChargementIsAlive();
}catch(Exception e){
logger.error("", e);
}finally{
try{
lastAliveDao.close();
}catch(Exception e)
{
logger.warn("Exception closing dao : ", e);
}
lastAliveDao = null;
}
}
}
DAO代码:
public class JpaLastAliveDao extends JpaDao<LastAlive, Integer> implements LastAliveDao {
@Override
public void sayChargementIsAlive() {
List<LastAlive> alives = this.list();
if(alives.size() == 0){
LastAlive alive = new LastAlive();
alive.setLastDateAliveChargement(Calendar.getInstance().getTime());
this.persist(alive);
}else{
LastAlive alive = alives.get(0);
alive.setLastDateAliveChargement(Calendar.getInstance().getTime());
this.update(alive);
}
}
和JpaDAO代码:
public abstract class JpaDao<T, PK extends Serializable>
implements Dao<T, PK>{
protected Class<T> entityClass;
protected EntityManagerFactory emf;
protected EntityManager em;
@SuppressWarnings("unchecked")
public JpaDao()
{
emf = JPAUtils.getInstance("facturator").getEmf();
em = emf.createEntityManager();
ParameterizedType genericSuperclass = (ParameterizedType)
getClass().getGenericSuperclass();
this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
}
public T persist(T t) {
this.em.getTransaction().begin();
this.em.persist(t);
this.em.getTransaction().commit();
return t;
}
public T findById(PK id) {
return this.em.find(entityClass, id);
}
public T update(T t) {
this.em.getTransaction().begin();
T entityPersist = this.em.merge(t);
this.em.getTransaction().commit();
return entityPersist;
}
@SuppressWarnings("unchecked")
public List<T> list()
{
return this.em.createQuery("SELECT t FROM "+entityClass.getName()+" t ").getResultList();
}
public void delete(T t) {
this.em.getTransaction().begin();
this.em.remove(t);
this.em.getTransaction().commit();
}
public void close(){
em.close();
}
}
答案 0 :(得分:0)
好的,我找到了原因。数据库服务器和程序服务器在2个不同的子掩码网络上配置:一个为255.255.255.0,另一个为255.255.255.255。
我发现它的SocketException没有显示在堆栈中,并带有初始日志设置。