Question on JDBC Failover
Posted by: Leo Chan
Date: January 24, 2005 01:37AM

Hi,

I have sucessfully setup 2 MySQL in 2 servers for replication on 2 different servers. But I have some questions on using Connector/J (failover) applying on them. I have read throught the documents and make a simple program as following.

import java.sql.*;
import java.util.Properties;

public class TestMysqlR{

public static void main(String[] args){
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Properties props = new Properties();
props.put("autoReconnect", "true");
props.put("roundRobinLoadBalance", "true");
props.put("failOverReadOnly","false");
props.put("user", "test");
props.put("password", "123456");

conn=DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306,192.168.0.96:3306/test",
props);
conn.setAutoCommit(true);
PreparedStatement ps =conn.prepareStatement("insert into TEST (COUNT, UPDATE_TIME) values(1, now())");
ps.executeUpdate();
ps.close();
System.out.println("first line insert");
Thread.sleep(30000); // sleep a while for me to have time to down the master MYSQL network connection
ps =conn.prepareStatement("insert into TEST (COUNT, UPDATE_TIME) values(1, now())");
ps.executeUpdate();
ps.close();
conn.close();

}catch(Exception e){
e.printStackTrace(System.err);
}
}
}

I have first issue a insert SQL to the Master Server and everything is fine. Then I sleep the program a while for me to have enough time to unplug Master server from the network. However, the JDBC only throw me Exception but do nothing on fail over to Slave Server. Is something I set wrong or I misunderstand the features provided. The failover only work is when I first unplugged the Master from network and then run the progam, it can change to use the Slave Server. I have dump out the Exception for first case.

com.mysql.jdbc.CommunicationsException: Communications link failure due to under
lying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Connection reset by peer.

STACKTRACE:

java.net.SocketException: Connection reset by peer.
at jrockit.net.SocketNativeIO.write(IIIZ)I(Unknown Source)
at jrockit.net.SocketNativeIO.write(III)I(Unknown Source)
at jrockit.io.NativeIO.write(Ljava.io.FileDescriptor;II)I(Unknown Source
)
at java.net.AbstractSocketImpl$2.write(II)V(Unknown Source)
at jrockit.io.NativeIOOutputStream.write(I[BI)V(Unknown Source)
at jrockit.io.NativeIOOutputStream.write([BII)V(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer()V(BufferedOutputStream.java
:66)
at java.io.BufferedOutputStream.flush()V(BufferedOutputStream.java:124)
at com.mysql.jdbc.MysqlIO.send(Lcom.mysql.jdbc.Buffer;I)V(MysqlIO.java:2
620)
at com.mysql.jdbc.MysqlIO.send(Lcom.mysql.jdbc.Buffer;)V(MysqlIO.java:25
51)
at com.mysql.jdbc.MysqlIO.sendCommand(ILjava.lang.String;Lcom.mysql.jdbc
.Buffer;ZLjava.lang.String;)Lcom.mysql.jdbc.Buffer;(MysqlIO.java:1512)
at com.mysql.jdbc.ServerPreparedStatement.serverPrepare(Ljava.lang.Strin
g;)V(ServerPreparedStatement.java:1485)
at com.mysql.jdbc.ServerPreparedStatement.<init>(Lcom.mysql.jdbc.Connect
ion;Ljava.lang.String;Ljava.lang.String;)V(ServerPreparedStatement.java:151)
at com.mysql.jdbc.Connection.prepareStatement(Ljava.lang.String;II)Ljava
.sql.PreparedStatement;(Connection.java:1309)
at com.mysql.jdbc.Connection.prepareStatement(Ljava.lang.String;)Ljava.s
ql.PreparedStatement;(Connection.java:1281)
at TestMysqlR.main([Ljava.lang.String;)V(TestMysqlR.java:35)


** END NESTED EXCEPTION **


at com.mysql.jdbc.MysqlIO.send(Lcom.mysql.jdbc.Buffer;I)V(MysqlIO.java:2
638)
at com.mysql.jdbc.MysqlIO.send(Lcom.mysql.jdbc.Buffer;)V(MysqlIO.java:25
51)
at com.mysql.jdbc.MysqlIO.sendCommand(ILjava.lang.String;Lcom.mysql.jdbc
.Buffer;ZLjava.lang.String;)Lcom.mysql.jdbc.Buffer;(MysqlIO.java:1512)
at com.mysql.jdbc.ServerPreparedStatement.serverPrepare(Ljava.lang.Strin
g;)V(ServerPreparedStatement.java:1485)
at com.mysql.jdbc.ServerPreparedStatement.<init>(Lcom.mysql.jdbc.Connect
ion;Ljava.lang.String;Ljava.lang.String;)V(ServerPreparedStatement.java:151)
at com.mysql.jdbc.Connection.prepareStatement(Ljava.lang.String;II)Ljava
.sql.PreparedStatement;(Connection.java:1309)
at com.mysql.jdbc.Connection.prepareStatement(Ljava.lang.String;)Ljava.s
ql.PreparedStatement;(Connection.java:1281)
at TestMysqlR.main([Ljava.lang.String;)V(TestMysqlR.java:35)



Thanks for your help in advance.

Options: ReplyQuote


Subject
Written By
Posted
Question on JDBC Failover
January 24, 2005 01:37AM
January 26, 2005 11:47PM
February 21, 2005 12:29AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.