简体   繁体   中英

Executing batch SQL updates via multithreading via Java

I am having a little bit of trouble trying to get my application to run SQL updates via multi threading in java.

I have divided it in to 3 method, the main method, runner, and runner 2, and was hoping I could have both run and runner2 methods working simultaneously, however this does not seem to be happening. I am clearly doing something wrong but unsure what?

package fedMerger;

//PREREQUISITES: ENSURE THE FOLLOWING ARE NOT DISABLED IN SERVICES MANAGEMENT WHEN RUNNING THIS UTILITY:
//SQL SERVER BROWER 
//SQL SERVER 
//SQL SERVER VSS WRITER

//BENCHMARK TEST - 11million merged in 77 minutes.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class FedMerger extends Thread {

    private String directory = "C:\\Users\\xxx\\Desktop\\Files\\";
    private String AgentfileName = "1.txt";
    private String otherFileName = "2.txt";
    public static Connection connection;
    private String mapperValue = "";
    private static String TimeStampTableName = "TimeStampTable";
    private static String timeStampColumn = "TIMESTAMP";
    private static String remainingDataColumn = "REMAINING";
    private static String user = "sa";
    private static String pass = "xxx";
    public static long timer;
    public String Timestampquery = "INSERT INTO " + TimeStampTableName + "(" + timeStampColumn + ","
            + remainingDataColumn + ") VALUES (?,?)";
    public static String dbURL = "jdbc:sqlserver://localhost\\SQLExpress;database=TIMESTAMP_ORGANISER;integratedSecurity=true";

    String getQuery() {
        return Timestampquery;
    }

    public static void main(String[] args) throws Exception {
        Connection conn = null;
        timer = System.currentTimeMillis();
        conn = DriverManager.getConnection(dbURL, user, pass);
        connection = conn;
        String createTimeStampTable = "CREATE TABLE " + TimeStampTableName + "(" + timeStampColumn + " varchar(max),"
                + remainingDataColumn + " varchar(max))";
        System.out.println("Tables & Columns created - Populating data...");
        conn.createStatement().executeUpdate(createTimeStampTable);
        Thread t1 = new Thread(new FedMerger());
        t1.run();
        Thread t2 = new Thread(new FedMerger());
        t2.run();
    }

    public void run() {
        Connection conn = connection;
        String mapperValue2 = "";
        int i = 0;
        int records = 0;
        try {
            BufferedReader agentFile = new BufferedReader(new FileReader(directory + AgentfileName));
            PreparedStatement statement = null;
            statement = conn.prepareStatement(Timestampquery);
            for (mapperValue2 = agentFile.readLine(); mapperValue2 != null; mapperValue2 = agentFile.readLine()) {
                i++;
                records++;
                if (!mapperValue2.isEmpty() && mapperValue2.length() > 5) {
                    statement.setString(1, mapperValue2.substring(0, 26));
                    statement.setString(2, mapperValue2.substring(26, mapperValue2.length()));
                    statement.addBatch();
                } else {// ignore blanks or white spaces
                    System.out.println("blank found - skipped");
                }
                if (i == 10) {// Populating 5000 records at a time
                    System.out.println("executing Agent - " + records + " records...");
                    statement.executeBatch();
                    statement.clearBatch();
                    i = 0;
                }
            }
            statement.executeBatch();
            statement.close();
            agentFile.close();

        } catch (SQLException | FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void runner2() throws SQLException, IOException {

        Connection conn = connection;

        try {// Database setup and file to be read.

            BufferedReader timeStampFile = new BufferedReader(new FileReader(directory + otherFileName));

            int i = 0;
            int records = 0;
            PreparedStatement ps = conn.prepareStatement(Timestampquery);
            // Dump FED info onto SQL
            for (mapperValue = timeStampFile.readLine(); mapperValue != null; mapperValue = timeStampFile.readLine()) {
                i++;
                records++;
                if (!mapperValue.isEmpty() && mapperValue.length() > 5) {
                    ps.setString(1, mapperValue.substring(0, 26));
                    ps.setString(2, mapperValue.substring(26, mapperValue.length()));
                    ps.addBatch();
                } else {// ignore blanks or white spaces
                    System.out.println("blank found - skipped");
                }
                if (i == 100000) {// Populating 10000 records at a time
                    System.out.println("executing timestamp - " + records + " records...");
                    ps.executeBatch();
                    ps.clearBatch();
                    i = 0;
                }
            }
            System.out.println("executing " + records + " records...");
            ps.executeBatch();
            ps.clearBatch();
            i = 0;
            // Dump AGENT FED info into same SQL
            System.out.print("Uploaded to database - Working SQL query");
            BufferedWriter writer = new BufferedWriter(new FileWriter(directory + "newfile" + "_MergedFinal.txt"));
            // Organise accordingly
            String retrieveData = "select " + timeStampColumn + "+" + remainingDataColumn + " as Data from "
                    + TimeStampTableName + " order by timestamp, case WHEN remaining LIKE '%agentStateEvent%' THEN -3 "
                    + "WHEN remaining LIKE '%TerminalConnectionCreated%' THEN -2 " + "ELSE -1 END";
            PreparedStatement stmt = conn.prepareStatement(retrieveData);
            ResultSet result = null;
            result = stmt.executeQuery();
            int j = 0;
            String results = "";
            System.out.println("Data organised, ready to output...");
            while (result.next()) {// SQL Query ran - Output data line by
                                    // line
                j++;
                System.out.println("outputing data - " + j);
                results = result.getString("data");
                writer.write(results + "\r\n");
                writer.flush();
            }
            writer.write(results + "\r\n");
            writer.flush();
            writer.close();
            System.out.println("Done - View at " + directory + "NewFile_MergedFinal.txt");
            conn.createStatement().executeUpdate("DROP TABLE " + TimeStampTableName);
            conn.close();
            timeStampFile.close();
            System.out.print("Complete - Time taken: " + ((TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())
                    - TimeUnit.MILLISECONDS.toMinutes(timer))) + " minutes");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

The runner2 Method is never called any where in your code. What you do is call run two times what you want to do is call run once and runner2 once.

Example Code:

Thread t1 = new Thread(){
    @Override
    public void run()
    {
        // Your Code goes here run
    }
    };

    Thread t2 = new Thread(){
    @Override
    public void run()
    {
        // Your Code goes here runner2
    }
    };

    t1.start();
    t2.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM