2020年9月23日 星期三

[java]使用okhttp3時發生SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.ssl.Alerts.getSSLException(Unknown Source)

at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)

at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)

at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)

at sun.security.ssl.Handshaker.processLoop(Unknown Source)

at sun.security.ssl.Handshaker.process_record(Unknown Source)

at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:268)

at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:238)

at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149)

at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192)

at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)

at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)

at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)

at okhttp3.RealCall.execute(RealCall.java:69)


 

解法

new OkHttpClient();改成getUnsafeOkHttpClient();

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;


/**

* okHttp3信任所有證書

* @return OkHttpClient

*/

public static OkHttpClient getUnsafeOkHttpClient() {

try {

final TrustManager[] trustAllCerts = new TrustManager[]{

new X509TrustManager() {

@Override

public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {

}

@Override

public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {

}

@Override

public java.security.cert.X509Certificate[] getAcceptedIssuers() {

return new java.security.cert.X509Certificate[]{};

}

}

};

final SSLContext sslContext = SSLContext.getInstance("SSL");

sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient.Builder builder = new OkHttpClient.Builder();

builder.sslSocketFactory(sslSocketFactory);


builder.hostnameVerifier(new HostnameVerifier() {

@Override

public boolean verify(String hostname, SSLSession session) {

return true;

}

});

return builder.build();

} catch (Exception e) {

throw new RuntimeException(e);

}

}






2020年6月11日 星期四

[java]java.sql.SQLException: I/O Error: Connection reset by peer: socket write error

java.sql.SQLException: I/O Error: Connection reset by peer: socket write error
at net.sourceforge.jtds.jdbc.TdsCore.executeSQL(TdsCore.java:1065)
at net.sourceforge.jtds.jdbc.TdsCore.microsoftPrepare(TdsCore.java:1191)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareSQL(ConnectionJDBC2.java:693)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:978)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)

主要原因還是在 Tomcat 的 connection pool 與 DB Server 斷線所引起。
重啟server即可

tomcat相關設定
maxWait="3000"  從池中取連線的最大等待時間,單位ms.
initialSize="10"  初始化連線
maxIdle="60"  最大空閒連線
minIdle="10"  最小空閒連線
maxActive="80"  最大活動連線
validationQuery = "SELECT 1"  驗證使用的SQL語句  
testWhileIdle = "true"  指明連線是否被空閒連接回收器(如果有)進行檢驗.如果檢測失敗,則連線將被從池中去除
testOnBorrow = "false"  借出連線時不要測試,否則很影響效能
timeBetweenEvictionRunsMillis = "30000"  每30秒執行一次空閒連接回收器
minEvictableIdleTimeMillis = "1800000"  池中的連線空閒30分鐘後被回收
numTestsPerEvictionRun="10"  在每次空閒連接回收器執行緒(如果有)執行時檢查的連線數量

removeAbandoned="true"  連線洩漏回收引數,當可用連線數少於3個時才執行  
removeAbandonedTimeout="200"  連線洩漏回收參數,200秒,洩露的連線可以被刪除的超時值。當連線時間大於200秒,不等資料庫回應就砍連線。
removeAbandonedTimeout        屬性是設定資料庫連線被釋最多空閒時間多少秒之後設定為空閒。預設移除廢棄連線的時間為300秒。





2020年5月29日 星期五

[java]NoClassDefFoundError

問題
Caused by: java.lang.NoClassDefFoundError: org/apache/http/HttpEntity

NoClassDefFoundError 錯誤發生原因
因為程式在執行時,找不到此class導致的錯誤。此時jvm就會拋出NoClassDefFoundError

如何解決
找出此class對應的jar,載入到classpath中即可

[攝影]空拍小技巧

拍照小技巧
  • 線條
  • 尋找形狀
  • 重複場景
  • 分割畫面
  • 找亮點
影片小技巧
  • 飛高飛遠
  • 鳥瞰飛高
  • 鳥瞰雲台移動
  • 水平運鏡(小心障礙物)
  • 空拍機上升同時雲台往下;空拍機下降同時雲台往上

2020年5月28日 星期四

eclipse 快速提醒相關聯指令

看到前輩key 任何字,馬上就有相關聯的提示出現
不同於alt + /
立馬詢問怎麼調整

點選 Menu: Windows > Preferences > Java > Editor > Content Assist
在Auto activartion triggers for Java填入『.@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ











結果

^