Description
`
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import org.tarantool.SocketChannelProvider;
import org.tarantool.TarantoolClient;
import org.tarantool.TarantoolClientConfig;
import org.tarantool.TarantoolClientImpl;
public class TarantoolSQLTest {
private static String url = "";
private static final String driverClass = "org.tarantool.jdbc.SQLDriver";
private static String makeUrl(String host, int port) {
return "jdbc:tarantool://" + host + ":" + port; //No I18N
}
private static final String sql = "select * from test";
public static void main(String []args) {
Connection con = null;
PreparedStatement pStmt = null;
String host = "127.0.0.1";
int port = 3301;
System.out.println("SQL Client\n\n\n");
try {
url = makeUrl(host, port);
long total = System.currentTimeMillis();
Class.forName(driverClass).newInstance();
long contime = System.currentTimeMillis();
con = DriverManager.getConnection(url, "guest", "");
contime = System.currentTimeMillis() - contime;
con.setTransactionIsolation(Connection.TRANSACTION_NONE);
long preTime = System.currentTimeMillis();
pStmt = con.prepareStatement(sql);
preTime = System.currentTimeMillis() - preTime;
long extime = System.currentTimeMillis();
ResultSet rs = pStmt.executeQuery();
extime = System.currentTimeMillis() - extime;
total = System.currentTimeMillis() - total;
System.out.println("Total Time Taken : "+total);
System.out.println("Connection Time Taken : "+contime);
System.out.println("Prepare time taken : "+preTime);
System.out.println("Execution time Taken : "+extime);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("\n\n\nTarantool Client\n\n\n");
try {
SocketChannelProvider provider = new SocketChannelProviderImpl(host, port, 100);
TarantoolClientConfig config = new TarantoolClientConfig();
config.username = "guest";
config.password = "";
long total = System.currentTimeMillis();
long contime = System.currentTimeMillis();
TarantoolClient client = new TarantoolClientImpl(provider, config);
contime = System.currentTimeMillis() - contime;
int space = 518;
long extime = System.currentTimeMillis();
Object result = client.syncOps().select(space, 0, Arrays.asList(1), 0, 5000, 0);
extime = System.currentTimeMillis() - extime;
total = System.currentTimeMillis() - total;
System.out.println(result);
System.out.println("Total Time Taken : "+total);
System.out.println("Connection Time Taken : "+contime);
System.out.println("Execution time Taken : "+extime);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class SocketChannelProviderImpl implements SocketChannelProvider {
private String ip;
private int port;
private long timeout;
public SocketChannelProviderImpl(String ip, int port, long timeout) {
this.ip = ip;
this.port = port;
this.timeout = timeout;
}
public SocketChannel get(int retryNumber, Throwable lastError) {
try {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.socket().connect(new InetSocketAddress(this.ip, this.port),
(new Long(this.timeout)).intValue());
return socketChannel;
} catch (Throwable var4) {
lastError.fillInStackTrace();
return null;
}
}
}
`
This is the code i used to find the time taken between normal tarantool client and SQLConnection.
i can see a difference in time in getting connection.