前置準備
- 下載 SQL Server JDBC 驅動:
mssql-jdbc-.jar(微軟官方)或 jtds-1.3.1.jar。
- 將 Jar 放入 WebLogic 域庫目錄:
$DOMAIN_HOME/lib(全域可見)或打包進應用 WEB-INF/lib(隔離版本)。
控制台配置數據源(Generic Data Source)
- 登錄 WebLogic Admin Console(默認
https://host:7001/console)。
- 左側導航:Services → Data Sources → New → Generic Data Source。
- 基本屬性:
- Name:
JDBC_SQLServer_DS
- JNDI Name:
jdbc/SqlServerDS(應用通過此名查找)
- Database Type:MS SQL Server
- Database Driver:Microsoft's SQL Server Driver (Type 4) Versions: ... 或 Other 填
net.sourceforge.jtds.jdbc.Driver
- 事務選項:保持默認(Supports Global Transactions、One-Phase Commit、Emulate Two-Phase Commit),除非確需 XA。
- 連接屬性:
- Database Name:
YourDB
- Host Name:
192.168.1.100
- Port:
1433
- Database User Name:
app_user
- Password/Confirm Password:
******
- 測試連接:點擊 Test Configuration,成功顯示「Connection test succeeded」。
- 選擇目標:勾選目標 Server/Cluster(如
AdminServer 或 MyCluster)。
- Finish 完成創建。
進階調優參數(Connection Pool)
在數據源 → Connection Pool → Advanced 設置:
| 參數 | 建議值 | 說明 |
| Initial Capacity | 5-10 | 啟動時預創建連接數 |
| Max Capacity | 50-100 | 視應用並發與 DB 最大連接數而定 |
| Capacity Increment | 5 | 擴容步長 |
| Test Table Name | SQL SELECT 1 | 驗證連接有效性查詢 |
| Test Connections On Reserve | true | 借連接前測試,防死連接 |
| Seconds To Trust An Idle Pool Connection | 10 | 信任空閒連接秒數,減少測試開銷 |
| Statement Cache Type | LRU | 語句緩存策略 |
| Statement Cache Size | 20-50 | 緩存 PreparedStatement 數 |
應用中使用 (JNDI Lookup)
@Resource(name = "jdbc/SqlServerDS")
private DataSource dataSource;
// 或 Spring 配置
常見問題排查
- ClassNotFoundException:Jar 未放入
$DOMAIN_HOME/lib 或未重啟 AdminServer/ManagedServer。
- Connection refused:SQL Server 未啟用 TCP/IP(SQL Server Configuration Manager → Protocols → TCP/IP Enable)、防火牆攔截 1433、實例名非默認需加
instanceName 參數。
- SSL 加密錯誤:驅動 8.0+ 默認加密,若服務端無證書需在 URL 加
encrypt=false;trustServerCertificate=true(jTDS 不受影響)。
- XA 事務失敗:需在 SQL Server 啟用 MSDTC(Component Services → Computers → My Computer → Distributed Transaction Coordinator → Local DTC → Security → 勾選所有權限),並授予
SqlServer 服務賬號 DTC 訪問權。
copyright ©
萬盛學電腦網 all rights reserved