ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.bof.service;
 
import java.sql.SQLException;
import java.sql.Statement;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
 
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
 
public class BatchSqlService {
 
    private static BatchSqlService instance = null;
 
    private BatchSqlService() {
 
    }
 
    public static synchronized BatchSqlService getInstance() {
        if (instance == null) {
            instance = new BatchSqlService();
        }
 
        return instance;
    }
    
    public boolean executeUpdateSqls(String[] sqls) throws HibernateException, SQLException {
        boolean rs = false;
        Session session = HibernateSessionFactory.getSession();
        Statement st = null;
        try {
            st = session.connection().createStatement();
            for (int i = 0; i < sqls.length; i++) {
                st.addBatch(sqls[i]);
                if ((i + 1) % 200 == 0) {
                    st.executeBatch();
                }
            }
            
            if (sqls.length % 200 != 0) {
                st.executeBatch();
            }
            rs = true;
        } finally {
            if (st != null) {
                st.close();
                st = null;
            }
        }
        
        return rs;
    }
}