Oracle 大最插入数据 一段时间之后变慢问题解决方法

  因为系统业务需要,需要从数据中心导入12年的历史数据到系统。每天平均1000千左右。有一个表有200多个字段,基中还包括几个clob字段。导入方式是用http向服务器一天一天的请求数据。每次请求一天。不能请求太多天,因为服务器请求太多天,有可能返回不了数据。导致失败,Java的JDBC接口写入。每次写数据都是一条一条的写入。刚开始还算顺利,接口过来的数据都能写入进数据库。但问题出现在写入1个月数据左右就开始慢慢的出现卡顿的情况。写入一条记录的数据,有时10几秒,有几几分钟。最坏的时候,就一条数据几个小时还没有写入成功,导致整个同步进程严重拥堵。每次卡死之后,重启数据库服务又可以正常写入。但每次写入一段时候之后,又卡死。

试过以下方法都不见效:

1、停止所有的触发器

2、接收数据表使用nologging表。

3、分次提交,一次不要只提交一条记录。一次提交一天记录。

最后在网上找到了解决方法。也是分次提交,但不是使用executeUpdate()执行,而是使用了先addBatch(), 再executeBatch()的方式。

 

具体见代码中的:    public int executeBatch(String sql, List<Object[]> params) 方法。

附操作数据库的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package bh.ojdbc.util;
 
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.log4j.Logger;
 
import bh.getjzdata.Para;
import bh.util.SetRW;
import bh.util.Tool;
  
/**
 * 对jdbc的完整封装
 *
 */
public class JDBCUtil {
  
    private static Logger logger = Logger.getLogger(JDBCUtil.class);
    private static  String driver = null;
    private static  String url = null;
    private static  String username = null;
    private static  String password = null;
     
    private CallableStatement callableStatement = null;//创建CallableStatement对象     
    private Connection conn = null;
    private PreparedStatement pst = null;
    private ResultSet rst = null;
     
/*  static {   
        try {   
            // 加载数据库驱动程序   
            Class.forName(driver);   
        } catch (ClassNotFoundException e) {   
            System.out.println("加载驱动错误");   
            System.out.println(e.getMessage());   
        }   
    } */  
     
    public JDBCUtil(){
        this.driver = Para.oracle_driver;
        this.url = Para.oracle_url;
        this.username = Para.oracle_userid;
        this.password = Para.oracle_password;
     
    }
     
    public JDBCUtil(String driver,String url ,String username,String password) {
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;
    }
     
    /** 
     * 建立数据库连接 
     * @return 数据库连接 
     */   
    public Connection getConnection() {   
        try{
            if (this.conn!=null && !this.conn.isClosed()){
                return this.conn;
            }
        }
        catch(Exception e){
             
        }
        try {   
             // 加载数据库驱动程序   
 
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                System.out.println("加载驱动错误");   
                System.out.println(e.getMessage()); 
                e.printStackTrace();
                logger.error(Tool.getExceptionDetail(e));
            }
            // 获取连接   
            conn = DriverManager.getConnection(url, username,   
                    password);   
        } catch (SQLException e) {   
            System.out.println(e.getMessage());   
            logger.error(Tool.getExceptionDetail(e));
        }   
        return conn;   
    }   
     
    public int executeUpdate(String sql, Object[] params) {
        return executeUpdate(sql,params,true);
    }
    /** 
     * insert update delete SQL语句的执行的统一方法 
     * @param sql SQL语句 
     * @param params 参数数组,若没有参数则为null 
     * @return 受影响的行数 
     */   
    public int executeUpdate(String sql, Object[] params,boolean isCloseAll) {   
        // 受影响的行数   
        int affectedLine = 0;   
             
        try {   
            // 获得连接   
            conn = this.getConnection(); 
            if (!isCloseAll && conn.getAutoCommit()){
                conn.setAutoCommit(false);
                 
            }
            // 调用SQL    
            pst = conn.prepareStatement(sql);   
                 
            // 参数赋值   
            if (params != null) {   
                for (int i = 0; i < params.length; i++) {   
                    pst.setObject(i + 1, params[i]);   
                }   
            }   
            /*在此 PreparedStatement 对象中执行 SQL 语句,
                                          该语句必须是一个 SQL 数据操作语言(Data Manipulation Language,DML)语句,比如 INSERT、UPDATE 或 DELETE
                                          语句;或者是无返回内容的 SQL 语句,比如 DDL 语句。    */
            // 执行   
            affectedLine = pst.executeUpdate();
            
        } catch (SQLException e) {
            if (e.getMessage().indexOf("数据重复")<=0){
                System.out.println(e.getMessage());   
                logger.error(Tool.getExceptionDetail(e));
                logger.error(sql);
                if (params != null) {   
                    for (int i = 0; i < params.length; i++) {   
                        logger.error(params[i]);   
                    }   
                }
            }
            try{
                conn.commit();
            }
            catch(Exception ex){
                 
            }
            //closeAll();
        } finally {  
            try{
                 pst.close();
            }
            catch(Exception e){
                 
            }
            if (isCloseAll){
            // 释放资源   
                closeAll();   
            }
        }   
        return affectedLine;   
    }   
       
     
     
    /** 
     * insert update delete SQL语句的执行的统一方法 
     * @param sql SQL语句 
     * @param params 参数数组,若没有参数则为null 
     * @return 受影响的行数 
     */   
    public int executeBatch(String sql, List<Object[]> params) {   
        // 受影响的行数   
        int affectedLine = 0;   
             
        try {   
            // 获得连接   
            conn = this.getConnection(); 
            conn.setAutoCommit(false);
            // 调用SQL    
            pst = conn.prepareStatement(sql);   
                 
            // 参数赋值   
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {   
                    Object[] objs = params.get(i);
                    for (int j = 0; j < objs.length; j++) {   
                        pst.setObject(j + 1, objs[j]);   
                    }
                    pst.addBatch();
                }   
            }   
            /*在此 PreparedStatement 对象中执行 SQL 语句,
                                          该语句必须是一个 SQL 数据操作语言(Data Manipulation Language,DML)语句,比如 INSERT、UPDATE 或 DELETE
                                          语句;或者是无返回内容的 SQL 语句,比如 DDL 语句。    */
            // 执行   
            pst.executeBatch();
            
        } catch (SQLException e) {
            //如果出现异常。重新执行单个插入。
            try{
                conn.rollback();
            }
            catch(Exception ex){
                 
            }
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {   
                    this.executeUpdate(sql, params.get(i), false);
               }   
            }              
 
        } finally {  
            try{
                 pst.close();
            }
            catch(Exception e){
                 
            }
            closeAll();
        }   
        return affectedLine;   
    }   
     
    /** 
     * SQL 查询将查询结果直接放入ResultSet中 
     * @param sql SQL语句 
     * @param params 参数数组,若没有参数则为null 
     * @return 结果集 
     */   
    private ResultSet executeQueryRS(String sql, Object[] params) {   
        try {   
            // 获得连接   
            conn = this.getConnection();   
                 
            // 调用SQL   
            pst = conn.prepareStatement(sql);   
                 
            // 参数赋值   
            if (params != null) {   
                for (int i = 0; i < params.length; i++) {   
                    pst.setObject(i + 1, params[i]);   
                }   
            }   
                 
            // 执行   
            rst = pst.executeQuery();   
     
        } catch (SQLException e) {   
            System.out.println(e.getMessage());   
            logger.error(Tool.getExceptionDetail(e));
        }    
     
        return rst;   
    }   
         
    /** 
     * SQL 查询将查询结果:一行一列 
     * @param sql SQL语句 
     * @param params 参数数组,若没有参数则为null 
     * @return 结果集 
     */   
    public Object executeQuerySingle(String sql, Object[] params) {   
        Object object = null;   
        try {   
            // 获得连接   
            conn = this.getConnection();   
                 
            // 调用SQL   
            pst = conn.prepareStatement(sql);   
                 
            // 参数赋值   
            if (params != null) {   
                for (int i = 0; i < params.length; i++) {   
                    pst.setObject(i + 1, params[i]);   
                }   
            }   
                 
            // 执行   
            rst = pst.executeQuery();   
     
            if(rst.next()) {   
                object = rst.getObject(1);   
            }   
                 
        } catch (SQLException e) {   
            System.out.println(e.getMessage());
            logger.error(Tool.getExceptionDetail(e));
             
        } finally {   
            closeAll();   
        }   
     
        return object;   
    }   
     
    /** 
     * 获取结果集,并将结果放在List中 
     *  
     * @param sql  SQL语句 
     *         params  参数,没有则为null  
     * @return List 
     *                       结果集 
     */   
    public List<Object> excuteQuery(String sql, Object[] params) {   
        // 执行SQL获得结果集   
        ResultSet rs = executeQueryRS(sql, params);   
             
        // 创建ResultSetMetaData对象   
        ResultSetMetaData rsmd = null;   
             
        // 结果集列数   
        int columnCount = 0;   
        try {   
            rsmd = rs.getMetaData();   
                 
            // 获得结果集列数   
            columnCount = rsmd.getColumnCount();   
        } catch (SQLException e1) {   
            System.out.println(e1.getMessage());   
            logger.error(Tool.getExceptionDetail(e1));
        }   
     
        // 创建List   
        List<Object> list = new ArrayList<Object>();   
     
        try {   
            // 将ResultSet的结果保存到List中   
            while (rs.next()) {   
                Map<String, Object> map = new HashMap<String, Object>();   
                for (int i = 1; i <= columnCount; i++) {   
                    map.put(rsmd.getColumnLabel(i), rs.getObject(i));   
                }   
                list.add(map);//每一个map代表一条记录,把所有记录存在list中   
            }   
        } catch (SQLException e) {   
            System.out.println(e.getMessage());  
            logger.error(Tool.getExceptionDetail(e));
        } finally {   
            // 关闭所有资源   
            closeAll();   
        }   
     
        return list;   
    }   
         
    /** 
     * 存储过程带有一个输出参数的方法 
     * @param sql 存储过程语句 
     * @param params 参数数组 
     * @param outParamPos 输出参数位置 
     * @param SqlType 输出参数类型 
     * @return 输出参数的值 
     */   
    public Object excuteQuery(String sql, Object[] params,int outParamPos, int SqlType) {   
        Object object = null;   
        conn = this.getConnection();   
        try {   
            // 调用存储过程   
            // prepareCall:创建一个 CallableStatement 对象来调用数据库存储过程。
            callableStatement = conn.prepareCall(sql);   
                 
            // 给参数赋值   
            if(params != null) {   
                for(int i = 0; i < params.length; i++) {   
                    callableStatement.setObject(i + 1, params[i]);   
                }   
            }   
                 
            // 注册输出参数   
            callableStatement.registerOutParameter(outParamPos, SqlType);   
                 
            // 执行   
            callableStatement.execute();   
                 
            // 得到输出参数   
            object = callableStatement.getObject(outParamPos);   
                 
        } catch (SQLException e) {   
            System.out.println(e.getMessage());   
            logger.error(Tool.getExceptionDetail(e));
        } finally {   
            // 释放资源   
            closeAll();   
        }   
             
        return object;   
    }   
     
    /** 
     * 关闭所有资源 
     */   
    public  void closeAll() {   
        // 关闭结果集对象   
        if (rst != null) {   
            try {  
                if (!rst.isClosed()){
                    rst.close();   
                }
            } catch (SQLException e) {   
                System.out.println(e.getMessage());   
            }   
        }   
     
        // 关闭PreparedStatement对象   
        if (pst != null) {   
            try {   
                if (!pst.isClosed()){
                    pst.close();   
                }
            } catch (SQLException e) {   
                System.out.println(e.getMessage());
                logger.error(Tool.getExceptionDetail(e));
            }   
        }   
             
        // 关闭CallableStatement 对象   
        if (callableStatement != null) {   
            try {   
                callableStatement.close();   
            } catch (SQLException e) {   
                System.out.println(e.getMessage());   
                logger.error(Tool.getExceptionDetail(e));
            }   
        }   
     
        // 关闭Connection 对象   
        if (conn != null) {   
            try {   
                if(!conn.getAutoCommit()&&!conn.isClosed()){
                    conn.commit();
                }
                if(!conn.isClosed()){
                    conn.close();
                }
            } catch (SQLException e) {   
                System.out.println(e.getMessage());  
                logger.error(Tool.getExceptionDetail(e));
            }   
        }      
    }   
}

  

 

posted @   清风笑  阅读(4608)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示