springboot项目中使用双数据源,连接第二个mysql数据源的时候,为啥会爆出如下错误?
@Transactional(rollbackFor = Exception.class)
@Service
@Slf4j
public class RefrigerantJobService {
@Resource
private RefrigerantMapper refrigerantMapper;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
PreparedStatement pst = null;
public Integer getLock(){
Integer result = refrigerantMapper.updateLock(DateUtils.DateToString(new Date(),DateUtils.DATE_TO_STRING_yyyy_MM_dd),"refrigerant");
log.info("抢锁结果(1成功,0失败):"+result);
return result;
}
public void synchronize(){
Integer result = getLock();
//抢锁成功
if (result!=null&&result==1){
log.info("抢锁成功,执行同步GaschargeData表");
synchronizeGaschargeData();
}else {
log.info("抢锁失败,该节点不执行同步任务");
}
}
private void synchronizeGaschargeData(){
try {
conn = getConnection();
st = conn.createStatement();
//查询本地库最新的id
RefrigerantDTO refrigerant =refrigerantMapper.getMaxAutoId();
log.info("refrigerant"+refrigerant);
/*String sql = "select * from gascharge_data";
rs = st.executeQuery(sql);
while(rs.next()){
InjectionWasteDifference injectionWasteDifference = new InjectionWasteDifference();
injectionWasteDifference.setProjectId(projectId);
String prodCode = rs.getString("prod_code");
injectionWasteDifference.setProduceModel(rs.getString("prod_code"));
injectionWasteDifference.setMaterialNumber(rs.getString("zyh"));
injectionWasteDifference.setYield(rs.getString("finAmount"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
String time = rs.getString("InsertTime");
Date date = null;
date = format.parse(time);
injectionWasteDifference.setDate(date);
InjectionWasteDifference injectionWaste = new InjectionWasteDifference();
injectionWaste.setProdCode(prodCode);
injectionWaste.setDate(date);
long injectionTimes = injectionWasteDifferenceMapper.selProdCode(injectionWaste);
injectionWasteDifference.setInjectionTimes(String.valueOf(injectionTimes));
injectionWasteDifference.setIndustrialWaste("0");
injectionWasteDifferenceMapper.create(injectionWasteDifference);
}*/
}catch (Exception e){
log.info("",e);
}
}
private Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://1.1.1.1:3306/normal?useUnicode=true&characterEncoding=UTF8";
//驱动
String name = "com.mysql.cj.jdbc.Driver";
String user = "haaa";
String password = "haaa";
Connection conn = null;
// 指定连接类型
Class.forName(name);
// 获取连接
conn = DriverManager.getConnection(url, user, password);
return conn;
}
}
11:02:30.016 INFO [scheduling-1] [RefrigerantJobMine-24] mine同步gascharge_data表,开始同步时间2022-08-23 11:02:3011:02:30.733 INFO [scheduling-1] [RefrigerantJobService-30] 抢锁结果(1成功,0失败):1
11:02:30.733 INFO [scheduling-1] [RefrigerantJobService-37] 抢锁成功,执行同步GaschargeData表
11:02:32.765 INFO [scheduling-1] [RefrigerantJobService-77]
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.haier.material.service.service.material.RefrigerantJobService.getConnection(RefrigerantJobService.java:92)
at com.haier.material.service.service.material.RefrigerantJobService.synchronizeGaschargeData(RefrigerantJobService.java:48)
at com.haier.material.service.service.material.RefrigerantJobService.synchronize(RefrigerantJobService.java:38)
at com.haier.material.service.service.material.RefrigerantJobService$$FastClassBySpringCGLIB$$67d85f09.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.haier.material.service.service.material.RefrigerantJobService$$EnhancerBySpringCGLIB$$5251dd19.synchronize(<generated>)
at com.haier.material.service.job.RefrigerantJobMine.doWork(RefrigerantJobMine.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:544)
at com.mysql.cj.protocol.a.NativeProtocol.readServerCapabilities(NativeProtocol.java:496)
at com.mysql.cj.protocol.a.NativeProtocol.beforeHandshake(NativeProtocol.java:383)
at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1346)
at com.mysql.cj.NativeSession.connect(NativeSession.java:157)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
... 35 common frames omitted
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at com.mysql.cj.protocol.ReadAheadInputStream.fill(ReadAheadInputStream.java:107)
at com.mysql.cj.protocol.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:150)
at com.mysql.cj.protocol.ReadAheadInputStream.read(ReadAheadInputStream.java:180)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at com.mysql.cj.protocol.FullReadInputStream.readFully(FullReadInputStream.java:64)
at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:63)
at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:45)
at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:538)
... 41 common frames omitted
11:02:35.008 INFO [scheduling-1] [RefrigerantJobMine-24] mine同步gascharge_data表,开始同步时间2022-08-23 11:02:35
11:02:35.286 INFO [scheduling-1] [RefrigerantJobService-30] 抢锁结果(1成功,0失败):0
11:02:35.286 INFO [scheduling-1] [RefrigerantJobService-41] 抢锁失败,该节点不执行同步任务
回答:
这是连接失败的提示.
先测试一下看能不能连接到mysql服务器上.
以上是 springboot项目中使用双数据源,连接第二个mysql数据源的时候,为啥会爆出如下错误? 的全部内容, 来源链接: utcz.com/p/944650.html