Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用分析工具在java代码中查找outOfMemoryError的确切原因_Java_Linux_Jdbc - Fatal编程技术网

使用分析工具在java代码中查找outOfMemoryError的确切原因

使用分析工具在java代码中查找outOfMemoryError的确切原因,java,linux,jdbc,Java,Linux,Jdbc,嗨,我有一个java程序,可以使用JDBC同时访问远程和本地数据库 但我有一个例外: ->线程“thread-1964”java.lang.OutOfMemoryError中出现异常:java堆空间 现在我想使用任何分析工具,通过它我可以在我的代码中得到这个异常的确切原因 这是我的java程序 public class DBTestCases{ Connection localConnection; Connection remoteConnection; Conne

嗨,我有一个java程序,可以使用JDBC同时访问远程和本地数据库

但我有一个例外: ->线程“thread-1964”java.lang.OutOfMemoryError中出现异常:java堆空间

现在我想使用任何分析工具,通过它我可以在我的代码中得到这个异常的确切原因

这是我的java程序

public class DBTestCases{

    Connection localConnection; 
    Connection remoteConnection;
    Connection localCon;
    Connection remoteCon;
    List<Connection> connectionsList;
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = "root";
    String dbName = "myDB";
    String connectionUrl1= "jdbc:mysql://11.232.33:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
    String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";

    public List<Connection> createConnection() {

        try {
                    Class.forName(driver);
                    localCon = DriverManager.getConnection(connectionUrl2);
                    if(localCon != null)
                        System.out.println("connected to remote database at : "+new Date());
                    remoteCon = DriverManager.getConnection(connectionUrl1);
                    if(remoteCon != null)
                        System.out.println("connected to local database at : "+new Date());
                    connectionsList = new ArrayList<Connection>( 2 );
                    connectionsList.add( 0 , localCon );
                    connectionsList.add( 1 , remoteCon );
                } catch(ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                    } catch(SQLException sqle) {
                        sqle.printStackTrace();
                        }
        return connectionsList;
    }

    public void insert(){

        Runnable runnable = new Runnable(){
        public void run() {
        PreparedStatement ps1 = null;
        PreparedStatement ps2 = null;
        String sql = "insert into user1(name, address, created_date)" +
                                " values('johnsan', 'usa', '2013-08-04')";
        if(remoteConnection != null&&localConnection != null) {
            System.out.println("Database Connection Is Established");
            try {
                        ps1 = remoteConnection.prepareStatement(sql);
                        ps2 = localConnection.prepareStatement(sql);
                        int i = ps1.executeUpdate();
                        int k = ps2.executeUpdate();
                        if(i > 0) {
                            System.out.println("Data Inserted into remote database table Successfully");
                        }
                        if(k > 0) {
                            System.out.println("Data Inserted into local database table Successfully");
                        }
                    } catch (SQLException s) {
                            System.out.println("SQL code does not execute.");
                            s.printStackTrace();
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
            }
            System.out.println("Inserting values in db");
        }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public void retrieve(){

        Runnable runnable = new Runnable(){
        public void run() {
        try {
                    Statement st1 = localConnection.createStatement();
                    Statement st2 = remoteConnection.createStatement();
                    ResultSet res1 = st1.executeQuery("SELECT * FROM  user1");
                    ResultSet res2 = st2.executeQuery("SELECT * FROM  user1");
                    System.out.println("---------------------------Local Database------------------------");
                    while (res1.next()) {
                        Long i = res1.getLong("userId");
                        String s1 = res1.getString("name");
                        String s2 = res1.getString("address");
                        java.sql.Date d = res1.getDate("created_date");
                        System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
                    }
                    System.out.println("------------------------Remote Database---------------------");
                    while (res2.next()) {
                        Long i = res2.getLong("userId");
                        String s1 = res2.getString("name");
                        String s2 = res2.getString("address");
                        java.sql.Date d = res2.getDate("created_date");
                        System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
                    }
        } catch (SQLException s) {
                System.out.println("SQL code does not execute.");
                s.printStackTrace();
        }catch (Exception e) {
                e.printStackTrace();
        }
        }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public static void main(String[] args) {
        DBTestCases dbTestCases = new DBTestCases();
        List l = dbTestCases.createConnection();
        dbTestCases.localConnection = (Connection)l.get(0);
        dbTestCases.remoteConnection = (Connection)l.get(1);
        for(;;) {
            dbTestCases.insert();
            dbTestCases.countRows();
            dbTestCases.retrieve();
        }
    }
}
公共类DBTestCases{
连接本地连接;
连接远程连接;
连接localCon;
远程连接;
列表连接列表;
String driver=“com.mysql.jdbc.driver”;
字符串user=“root”;
字符串password=“root”;
字符串dbName=“myDB”;
String connectionUrl1=“jdbc:mysql://11.232.33:3306/“+dbName+”?user=“+user+”&password=“+password+”&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10”;
String connectionUrl2=“jdbc:mysql://localhost:3306/“+dbName+”?user=“+user+”&password=“+password+”&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10”;
公共列表createConnection(){
试一试{
Class.forName(驱动程序);
localCon=DriverManager.getConnection(connectionUrl2);
如果(localCon!=null)
System.out.println(“连接到远程数据库:“+new Date()”);
remoteCon=DriverManager.getConnection(connectionUrl1);
if(remoteCon!=null)
System.out.println(“连接到本地数据库:“+new Date()”);
connectionsList=newarraylist(2);
connectionsList.add(0,localCon);
connectionsList.add(1,remoteCon);
}捕获(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}捕获(SQLException sqle){
printStackTrace();
}
返回连接列表;
}
公开作废插入(){
Runnable Runnable=新的Runnable(){
公开募捐{
PreparedStatement ps1=null;
PreparedStatement ps2=null;
String sql=“插入user1(名称、地址、创建日期)”+
“价值观(‘约翰森’、‘美国’、‘2013-08-04’)”;
if(remoteConnection!=null&&localConnection!=null){
System.out.println(“已建立数据库连接”);
试一试{
ps1=remoteConnection.prepareStatement(sql);
ps2=localConnection.prepareStatement(sql);
int i=ps1.executeUpdate();
int k=ps2.executeUpdate();
如果(i>0){
System.out.println(“数据成功插入远程数据库表”);
}
如果(k>0){
System.out.println(“数据成功插入本地数据库表”);
}
}捕获(SQLS异常){
System.out.println(“SQL代码不执行”);
s、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
System.out.println(“在db中插入值”);
}
};
线程线程=新线程(可运行);
thread.start();
}
公共无效检索(){
Runnable Runnable=新的Runnable(){
公开募捐{
试一试{
语句st1=localConnection.createStatement();
语句st2=remoteConnection.createStatement();
ResultSet res1=st1.executeQuery(“从user1中选择*);
ResultSet res2=st2.executeQuery(“从用户1中选择*);
System.out.println(“-------------------------------本地数据库------------------------------------------”;
while(res1.next()){
Long i=res1.getLong(“userId”);
字符串s1=res1.getString(“名称”);
字符串s2=res1.getString(“地址”);
java.sql.Date d=res1.getDate(“创建日期”);
System.out.println(i+“\t\t”+s1+“\t\t”+s2+“\t\t”+d);
}
System.out.println(“---------------------------远程数据库------------------------------------------”;
while(res2.next()){
Long i=res2.getLong(“userId”);
字符串s1=res2.getString(“名称”);
字符串s2=res2.getString(“地址”);
java.sql.Date d=res2.getDate(“创建日期”);
System.out.println(i+“\t\t”+s1+“\t\t”+s2+“\t\t”+d);
}
}捕获(SQLS异常){
System.out.println(“SQL代码不执行”);
s、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
};
线程线程=新线程(可运行);
thread.start();
}
公共静态void main(字符串[]args){
DBTestCases DBTestCases=新的DBTestCases();
List l=dbTestCases.createConnection();
dbTestCases.localConnection=(Connection)l.get(0);
dbTestCases.remoteConnection=(Connection)l.get(1);
对于(;;){
dbTestCases.insert();
dbTestCases.countRows();
dbTestCases.retrieve();
}
}
}
请任何人都可以帮助我,哪一个是最好的工具使用,我如何使用它,以及任何链接。我使用的是linux操作系统

我想我正在为的每个调用启动一个新线程
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
Statement st1 = localConnection.createStatement();
Statement st2 = remoteConnection.createStatement();
ResultSet res1 = st1.executeQuery("SELECT * FROM  user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM  user1");
public class DBTestCases {

    Connection localConnection;
    Connection remoteConnection;
    Connection localCon;
    Connection remoteCon;
    List<Connection> connectionsList;
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = "root";
    String dbName = "myDB";
    String connectionUrl1= "jdbc:mysql://11.232.33:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
    String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";

    public List<Connection> createConnection() {

        try {
            Class.forName(driver);
            localCon = DriverManager.getConnection(connectionUrl2);
            if(localCon != null)
                System.out.println("connected to remote database at : "+new Date());
            remoteCon = DriverManager.getConnection(connectionUrl1);
            if(remoteCon != null)
                System.out.println("connected to local database at : "+new Date());
            connectionsList = new ArrayList<Connection>( 2 );
            connectionsList.add( 0 , localCon );
            connectionsList.add( 1 , remoteCon );
        } catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch(SQLException sqle) {
            sqle.printStackTrace();
        }
        return connectionsList;
    }

    public void insert(){

        Runnable runnable = new Runnable(){
            public void run() {
                PreparedStatement ps1 = null;
                PreparedStatement ps2 = null;
                String sql = "insert into user1(name, address, created_date)" +
                        " values('johnsan', 'usa', '2013-08-04')";
                if(remoteConnection != null&&localConnection != null) {
                    System.out.println("Database Connection Is Established");
                    try {
                        ps1 = remoteConnection.prepareStatement(sql);
                        ps2 = localConnection.prepareStatement(sql);
                        int i = ps1.executeUpdate();
                        int k = ps2.executeUpdate();
                        if(i > 0) {
                            System.out.println("Data Inserted into remote database table Successfully");
                        }
                        if(k > 0) {
                            System.out.println("Data Inserted into local database table Successfully");
                        }
                    } catch (SQLException s) {
                        System.out.println("SQL code does not execute.");
                        s.printStackTrace();
                    }catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (ps1 != null) {
                            try {
                                ps1.close();
                            } catch (SQLException ex) {
                                System.out.println("Cannot close ps1 statement.");
                            }
                        }

                        if (ps2 != null) {
                            try {
                                ps2.close();
                            } catch (SQLException ex) {
                                System.out.println("Cannot close ps2 statement.");
                            }
                        }
                    }
                }
                System.out.println("Inserting values in db");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public void retrieve(){

        Runnable runnable = new Runnable(){
            public void run() {
                Statement st1 = null;
                Statement st2 = null;
                ResultSet res1 = null;
                ResultSet res2 = null;
                try {
                    st1 = localConnection.createStatement();
                    st2 = remoteConnection.createStatement();
                    res1 = st1.executeQuery("SELECT * FROM  user1");
                    res2 = st2.executeQuery("SELECT * FROM  user1");
                    System.out.println("---------------------------Local Database------------------------");
                    while (res1.next()) {
                        Long i = res1.getLong("userId");
                        String s1 = res1.getString("name");
                        String s2 = res1.getString("address");
                        java.sql.Date d = res1.getDate("created_date");
                        System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
                    }
                    System.out.println("------------------------Remote Database---------------------");
                    while (res2.next()) {
                        Long i = res2.getLong("userId");
                        String s1 = res2.getString("name");
                        String s2 = res2.getString("address");
                        java.sql.Date d = res2.getDate("created_date");
                        System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
                    }
                } catch (SQLException s) {
                    System.out.println("SQL code does not execute.");
                    s.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (res1 != null) {
                        try {
                            res1.close();
                        } catch (SQLException ex) {
                            System.out.println("Cannot close res1 result set.");
                        }
                    }

                    if (st1 != null) {
                        try {
                            st1.close();
                        } catch (SQLException ex) {
                            System.out.println("Cannot close st1 statement.");
                            ex.printStackTrace();
                        }
                    }

                    if (res2 != null) {
                        try {
                            res2.close();
                        } catch (SQLException ex) {
                            System.out.println("Cannot close res2 result set.");
                        }
                    }

                    if (st2 != null) {
                        try {
                            st2.close();
                        } catch (SQLException ex) {
                            System.out.println("Cannot close st2 statement.");
                            ex.printStackTrace();
                        }
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public static void main(String[] args) {
        DBTestCases dbTestCases = new DBTestCases();
        List l = dbTestCases.createConnection();
        dbTestCases.localConnection = (Connection)l.get(0);
        dbTestCases.remoteConnection = (Connection)l.get(1);
        for(;;) {
            dbTestCases.insert();
            dbTestCases.countRows();
            dbTestCases.retrieve();
        }
    }
}
ResultSet res1 = st1.executeQuery("SELECT * FROM  user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM  user1");
ResultSet res1 = st1.executeQuery("SELECT created_date FROM  user1");
ResultSet res2 = st2.executeQuery("SELECT created_date FROM  user1");