⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcbench.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        return ret;    }    public static int getRandomID(int type) {        int min = 0,            max = 0;        switch (type) {            case TELLER :                max = ntellers * tps - 1;                break;            case BRANCH :                max = nbranches * tps - 1;                break;            case ACCOUNT :                max = naccounts * tps - 1;                break;        }        return (getRandomInt(min, max));    }    public static Connection connect(String DBUrl, String DBUser,                                     String DBPassword) {        try {            Connection conn = DriverManager.getConnection(DBUrl, DBUser,                DBPassword);            return conn;        } catch (Exception E) {            System.out.println(E.getMessage());            E.printStackTrace();        }        return null;    }    public static void connectClose(Connection c) {        if (c == null) {            return;        }        try {            c.close();        } catch (Exception E) {            System.out.println(E.getMessage());            E.printStackTrace();        }    }    void checkSums(Connection conn) throws Exception {        Statement st1 = null;        ResultSet rs  = null;        int       bbalancesum;        int       tbalancesum;        int       abalancesum;        int       deltasum;        try {            st1 = conn.createStatement();            rs  = st1.executeQuery("select sum(bbalance) from branches");            rs.next();            bbalancesum = rs.getInt(1);            rs.close();            rs = st1.executeQuery("select sum(tbalance) from tellers");            rs.next();            tbalancesum = rs.getInt(1);            rs.close();            rs = st1.executeQuery("select sum(abalance) from accounts");            rs.next();            abalancesum = rs.getInt(1);            rs.close();            rs = st1.executeQuery("select sum(delta) from history");            rs.next();            deltasum = rs.getInt(1);            rs.close();            rs = null;            st1.close();            st1 = null;            if (abalancesum != bbalancesum || bbalancesum != tbalancesum                    || tbalancesum != deltasum) {                System.out.println("sums don't match!");            } else {                System.out.println("sums match!");            }            System.out.println(abalancesum + " " + bbalancesum + " "                               + tbalancesum + " " + deltasum);        } finally {            if (st1 != null) {                st1.close();            }        }    }    class ClientThread extends Thread {        int               ntrans = 0;        Connection        Conn;        PreparedStatement pstmt1 = null;        PreparedStatement pstmt2 = null;        PreparedStatement pstmt3 = null;        PreparedStatement pstmt4 = null;        PreparedStatement pstmt5 = null;        public ClientThread(int number_of_txns, String url, String user,                            String password) {            ntrans = number_of_txns;            Conn   = connect(url, user, password);            if (Conn == null) {                return;            }            try {                if (transactions) {                    Conn.setAutoCommit(false);                }                if (prepared_stmt) {                    String Query;                    Query  = "UPDATE accounts ";                    Query  += "SET     Abalance = Abalance + ? ";                    Query  += "WHERE   Aid = ?";                    pstmt1 = Conn.prepareStatement(Query);                    Query  = "SELECT Abalance ";                    Query  += "FROM   accounts ";                    Query  += "WHERE  Aid = ?";                    pstmt2 = Conn.prepareStatement(Query);                    Query  = "UPDATE tellers ";                    Query  += "SET    Tbalance = Tbalance + ? ";                    Query  += "WHERE  Tid = ?";                    pstmt3 = Conn.prepareStatement(Query);                    Query  = "UPDATE branches ";                    Query  += "SET    Bbalance = Bbalance + ? ";                    Query  += "WHERE  Bid = ?";                    pstmt4 = Conn.prepareStatement(Query);                    Query  = "INSERT INTO history(Tid, Bid, Aid, delta) ";                    Query  += "VALUES (?,?,?,?)";                    pstmt5 = Conn.prepareStatement(Query);                }            } catch (Exception E) {                System.out.println(E.getMessage());                E.printStackTrace();            }        }        public void run() {            while (ntrans-- > 0) {                int account = JDBCBench.getRandomID(ACCOUNT);                int branch  = JDBCBench.getRandomID(BRANCH);                int teller  = JDBCBench.getRandomID(TELLER);                int delta   = JDBCBench.getRandomInt(0, 1000);                doOne(branch, teller, account, delta);                incrementTransactionCount();            }            if (prepared_stmt) {                try {                    if (pstmt1 != null) {                        pstmt1.close();                    }                    if (pstmt2 != null) {                        pstmt2.close();                    }                    if (pstmt3 != null) {                        pstmt3.close();                    }                    if (pstmt4 != null) {                        pstmt4.close();                    }                    if (pstmt5 != null) {                        pstmt5.close();                    }                } catch (Exception E) {                    System.out.println(E.getMessage());                    E.printStackTrace();                }            }            connectClose(Conn);            Conn = null;        }        /*         **  doOne() - Executes a single TPC BM B transaction.         */        int doOne(int bid, int tid, int aid, int delta) {            int aBalance = 0;            if (Conn == null) {                incrementFailedTransactionCount();                return 0;            }            try {                if (prepared_stmt) {                    pstmt1.setInt(1, delta);                    pstmt1.setInt(2, aid);                    pstmt1.executeUpdate();                    pstmt1.clearWarnings();                    pstmt2.setInt(1, aid);                    ResultSet RS = pstmt2.executeQuery();                    pstmt2.clearWarnings();                    while (RS.next()) {                        aBalance = RS.getInt(1);                    }                    pstmt3.setInt(1, delta);                    pstmt3.setInt(2, tid);                    pstmt3.executeUpdate();                    pstmt3.clearWarnings();                    pstmt4.setInt(1, delta);                    pstmt4.setInt(2, bid);                    pstmt4.executeUpdate();                    pstmt4.clearWarnings();                    pstmt5.setInt(1, tid);                    pstmt5.setInt(2, bid);                    pstmt5.setInt(3, aid);                    pstmt5.setInt(4, delta);                    pstmt5.executeUpdate();                    pstmt5.clearWarnings();                } else {                    Statement Stmt  = Conn.createStatement();                    String    Query = "UPDATE accounts ";                    Query += "SET     Abalance = Abalance + " + delta + " ";                    Query += "WHERE   Aid = " + aid;                    int res = Stmt.executeUpdate(Query);                    Stmt.clearWarnings();                    Query = "SELECT Abalance ";                    Query += "FROM   accounts ";                    Query += "WHERE  Aid = " + aid;                    ResultSet RS = Stmt.executeQuery(Query);                    Stmt.clearWarnings();                    while (RS.next()) {                        aBalance = RS.getInt(1);                    }                    Query = "UPDATE tellers ";                    Query += "SET    Tbalance = Tbalance + " + delta + " ";                    Query += "WHERE  Tid = " + tid;                    Stmt.executeUpdate(Query);                    Stmt.clearWarnings();                    Query = "UPDATE branches ";                    Query += "SET    Bbalance = Bbalance + " + delta + " ";                    Query += "WHERE  Bid = " + bid;                    Stmt.executeUpdate(Query);                    Stmt.clearWarnings();                    Query = "INSERT INTO history(Tid, Bid, Aid, delta) ";                    Query += "VALUES (";                    Query += tid + ",";                    Query += bid + ",";                    Query += aid + ",";                    Query += delta + ")";                    Stmt.executeUpdate(Query);                    Stmt.clearWarnings();                    Stmt.close();                }                if (transactions) {                    Conn.commit();                }                return aBalance;            } catch (Exception E) {                if (verbose) {                    System.out.println("Transaction failed: "                                       + E.getMessage());                    E.printStackTrace();                }                incrementFailedTransactionCount();                if (transactions) {                    try {                        Conn.rollback();                    } catch (SQLException E1) {}                }            }            return 0;        }    /* end of DoOne         */    }    /* end of class ClientThread */    class MemoryWatcherThread extends Thread {        long    min          = 0;        long    max          = 0;        boolean keep_running = true;        public MemoryWatcherThread() {            this.reset();            keep_running = true;        }        public void reset() {            System.gc();            long currentFree  = Runtime.getRuntime().freeMemory();            long currentAlloc = Runtime.getRuntime().totalMemory();            min = max = (currentAlloc - currentFree);        }        public void end() {            keep_running = false;        }        public void run() {            while (keep_running) {                long currentFree  = Runtime.getRuntime().freeMemory();                long currentAlloc = Runtime.getRuntime().totalMemory();                long used         = currentAlloc - currentFree;                if (used < min) {                    min = used;                }                if (used > max) {                    max = used;                }                try {                    sleep(100);                } catch (InterruptedException E) {}            }        }    }    /* end of class MemoryWatcherThread */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -