Testcontainers performance slower than plain docker - liquibase

I have the following scripted out and it works, but it was suggested that I try using testcontainers.
function die { mvn docker:stop ; exit 1; }
function initializeDatabase {
docker exec -it mysql-1 mysql -uroot -proot \
-e "DROP USER IF EXISTS 'myuser'#'%';" \
-e "DROP DATABASE IF EXISTS mydb;" \
-e "CREATE DATABASE mydb;" \
-e "CREATE USER 'myuser'#'%' IDENTIFIED WITH mysql_native_password BY 'mypassword';" \
-e "GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'#'%';"
}
mvn docker:start && sleep 10
initializeDatabase || die
mvn liquibase:update#execution1 || die
initializeDatabase || die
mvn liquibase:update#execution2 || die
initializeDatabase || die
mvn liquibase:update#execution3 || die
initializeDatabase || die
mvn liquibase:update#execution4 || die
initializeDatabase || die
mvn liquibase:update#execution5 || die
initializeDatabase || die
mvn liquibase:update#execution6 || die
mvn docker:stop
The above takes about 6 minutes to run. It starts a docker container running mysql and initializes the database and runs some liquibase migrations against it 6 different times, mimicking 6 different environments. It takes a little over 6 minutes to run. From Java, using testcontainers and the liquibase java API, it takes over 10 minutes to run. Any thoughts on how I can improve the performance of my testcontainers implementation? I don't think it's liquibase because it's the same liquibase code being invoked behind the scenes.
Below is my code:
import liquibase.Liquibase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.SearchPathResourceAccessor;
import org.junit.*;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SimpleMySQLTest {
#ClassRule
public static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:latest"))
.withEnv("TESTCONTAINERS_REUSE_ENABLED", "true")
.withEnv("TESTCONTAINERS_CHECKS_DISABLE", "true")
.withEnv("MYSQL_ROOT_PASSWORD", "root")
.withEnv("MYSQL_ROOT_HOST", "%")
.withDatabaseName("root")
.withUsername("root")
.withPassword("root");
#BeforeClass
public static void beforeClass() {
mysql.start();
}
#AfterClass
public static void afterClass() {
mysql.stop();
}
#Test
public void NAMSDev() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSDev/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
#Before
public void initializeDatabase() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl(), "root", "root")) {
Statement statement = connection.createStatement();
statement.execute("DROP USER IF EXISTS 'rms'#'%';");
statement.execute("DROP DATABASE IF EXISTS rms;");
statement.execute("CREATE DATABASE rms;");
statement.execute("CREATE USER 'rms'#'%' IDENTIFIED WITH mysql_native_password BY 'rms';");
statement.execute("GRANT ALL PRIVILEGES ON rms.* TO 'rms'#'%';");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
#Test
public void NAMSQc() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSQc/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
#Test
public void NAMSProd() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSProd/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
#Test
public void NORDICSDev() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSDev/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
#Test
public void NORDICSQc() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSQc/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
#Test
public void NORDICSProd() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSProd/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
}

Related

Renci.SshNet : "server response does not contain ssh protocol identification"

I'm working with the Renci SSH.Net library on a WPF application and I'm having an issue with using the SFTP client. When the user tries to connect to download some files from the SFTP server he gets the message shown below:
Server response does not contain ssh protocol identification
It doesn't appear to be something specific with the server as I'm able to connect and download the files just fine on my development desktop and a secondary laptop. The same application is able to connect over SSH and run commands without issue, it's just the SFTP connection that appears to be the problem. I'm looking for a little guidance as to where to begin troubleshooting this.
Code for SFTP shown below:
void DownloadPlogs()
{
try
{
SftpClient SFTP;
if (GV.UseCustomPort && GV.CustomPort > 0 && GV.CustomPort < 65535)
{
SFTP = new SftpClient(GV.IpAddress, GV.CustomPort, GV.Username, GV.Password);
}
else
{
SFTP = new SftpClient(GV.IpAddress, 22, GV.Username, "");
}
SFTP.Connect();
DownloadDirectory(SFTP, "/PLOG", Directory.GetCurrentDirectory() + #"\PLOG");
ZipFile.CreateFromDirectory("PLOG", String.Format("{0} - {1} PLOGS.zip", GV.IpAddress, DateTime.Now.ToString("yyyyMMddHHmmss")));
Directory.Delete(Directory.GetCurrentDirectory() + #"\PLOG", true);
SFTP.Disconnect();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Getting PLOGS");
}
}
void DownloadDirectory(SftpClient Client, string Source, string Destination)
{
var Files = Client.ListDirectory(Source);
foreach (var File in Files)
{
if (!File.IsDirectory && !File.IsSymbolicLink)
{
DownloadFile(Client, File, Destination);
}
else if (File.IsSymbolicLink)
{
//Ignore
}
else if (File.Name != "." && File.Name != "..")
{
var Dir = Directory.CreateDirectory(System.IO.Path.Combine(Destination, File.Name));
DownloadDirectory(Client, File.FullName, Dir.FullName);
}
}
}
void DownloadFile(SftpClient Client, Renci.SshNet.Sftp.SftpFile File, string Directory)
{
using (Stream FileStream = System.IO.File.OpenWrite(System.IO.Path.Combine(Directory, File.Name)))
{
Client.DownloadFile(File.FullName, FileStream);
}
}
Code for SSH below:
public SshConnection(string Host, int Port, string Username, string Password)
{
myClient = new SshClient(Host, Port, Username, Password);
myClient.KeepAliveInterval = new TimeSpan(0, 0, 5);
myClient.HostKeyReceived += myClient_HostKeyReceived;
myClient.ErrorOccurred += myClient_ErrorOccurred;
}
void myClient_ErrorOccurred(object sender, Renci.SshNet.Common.ExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "SSH Error Occurred");
}
void myClient_HostKeyReceived(object sender, Renci.SshNet.Common.HostKeyEventArgs e)
{
e.CanTrust = true;
}
public async void Connect()
{
Task T = new Task(() =>
{
try
{
myClient.Connect();
}
catch (System.Net.Sockets.SocketException)
{
MessageBox.Show("Invalid IP Address or Hostname", "SSH Connection Error");
}
catch (Renci.SshNet.Common.SshAuthenticationException ex)
{
MessageBox.Show(ex.Message, "SSH Authentication Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
MessageBox.Show(ex.GetType().ToString());
OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
}
});
T.Start();
await T;
if (T.IsCompleted)
{
OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
}
}
public void Disconnect()
{
try
{
myClient.Disconnect();
OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
}
}
public void SendData(string Data)
{
try
{
if (Data.EndsWith("\r\n"))
{
RunCommandAsync(Data, SshCommandRx);
}
else
{
RunCommandAsync(String.Format("{0}\r\n",Data), SshCommandRx);
}
//SshCommand Command = myClient.RunCommand(Data);
//OnDataReceived(this, new DataEventArgs(Command.Result));
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
}
}
private async void RunCommandAsync(String Data, SshCommandCallback Callback)
{
Task<SshCommand> T = new Task<SshCommand>(() =>
{
try
{
SshCommand Command = myClient.RunCommand(Data);
return Command;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
return null;
}
});
T.Start();
await T;
if (T.IsCompleted)
{
Callback(this, T.Result);
}
}
private void SshCommandRx(SshConnection C, SshCommand Command)
{
if (Command != null)
{
string Rx = Command.Result;
//if (Rx.StartsWith(Command.CommandText))
//{
// Rx = Rx.Remove(0, Command.CommandText.Length);
//}
while (Rx.EndsWith("\r\n\r\n") == false)
{
Rx += "\r\n";
}
OnDataReceived(this, new DataEventArgs(Rx));
}
}
I solve it for my self only with connections retrying attempts. Didn't find what exactly the issue is, but have this connection issue many times.
Example:
int attempts = 0;
do
{
try
{
client.Connect();
}
catch (Renci.SshNet.Common.SshConnectionException e)
{
attempts++;
}
} while (attempts < _connectiontRetryAttempts && !client.IsConnected);
I experienced the same odd error message when attempting to connect to a SFTP server while using the SSH.NET library in a program on the server. The problem did not appear while testing from my development machine.
The solution was to have our server team add the IP address of the server into the hosts.allow file on the SFTP Linux server.

Remote Glassfish JMS lookup

I want to connect to my Glassfish Server at XX.XX.XX.XX:XXXX which is running on a ubuntu machine on our Server.
I want to send messages and then receive them.
My Receiver looks like this:
public JMS_Topic_Receiver(){
init();
}
public List<TextMessage> getCurrentMessages() { return _currentMessages; }
private void init(){
env.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
env.put("org.omg.CORBA.ORBInitialHost", "10.10.32.14");
env.put("org.omg.CORBA.ORBInitialPort", "8080");
try {
ctx = new InitialContext(env); // NamingException
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void subscribeTopic(String topicName, String userCredentials) {
try {
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("myTopicConnectionFactory");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
try {
String temp = InetAddress.getLocalHost().getHostName();
topicConnection.setClientID(temp);
} catch (UnknownHostException e) {
e.printStackTrace();
}
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) ctx.lookup(topicName);
topicConnection.start();
TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, userCredentials);
topicSubscriber.setMessageListener(new MyMessageListener());
}
catch(NamingException | JMSException ex)
{
ex.printStackTrace();
}
}
And my Sender looks like this:
public JMS_Topic_Sender(){
init();
}
private void init(){
env.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
env.put("org.omg.CORBA.ORBHost","10.10.32.14");
env.put("org.omg.CORBA.ORBPort","8080");
try {
ctx = new InitialContext(env);
} catch (NamingException e) {
e.printStackTrace();
}
}
public void sendMessage(String message, String topicName) {
try {
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("myTopicConnectionFactory");
if (topicConnectionFactory != null) {
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) ctx.lookup(topicName);
topicConnection.start();
TopicPublisher topicPublisher = topicSession.createPublisher(topic);
TextMessage tm = topicSession.createTextMessage(message);
topicPublisher.send(tm);
topicSession.close();
topicConnection.stop();
topicConnection.close();
}
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
I got most of this code from various tutorials online.
Now when I want to do the lookup aka. -> ctx.lookup("myTopicConnectionFactory");
I get all sorts of errors thrown:
INFO: HHH000397: Using ASTQueryTranslatorFactory
org.omg.CORBA.COMM_FAILURE: FEIN: 00410008: Connection abort vmcid: OMG minor code: 8 completed: Maybe
javax.naming.NamingException: Lookup failed for 'myTopicConnectionFactory' in SerialContext ........
My question here is, how do I do the lookup correctly?
My guess is that my Propertys (env) are incorrect and I need to change thouse, but I dont know to what?
Also, it works if i run my Glassfish localy on my own Computers localhost.
When im using localhost as adress and 8080 as port.

can we automate Green Screen through Selenium or QTP?

we have customer provide architecture where they have use i series DB (green screen) and we have DB testing in scope for the contract. can some one help me to get more idea on how and which tool will be best fit for automation of these controls.
Thanks in Advance.
I suggest you to explore the possibilities of the java jdbc architecture to access and retrieve values from the database. You can use the the retrieved values to validate. :)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseConnections {
private Connection connection = null;
public Connection getDBConnectionQKB9() {
System.out
.println("DATABASE CONNECTION - Oracle JDBC Connection initialization");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(url, user, password);
if (connection != null) {
System.out
.println("DATABASE CONNECTION - Gained control - QKB9 database");
} else {
System.out
.println("DATABASE CONNECTION - Failed to make connection - QKB9 database");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getData() {
try {
PreparedStatement pt= connection.prepareStatement(query);
ResultSet rs = pt.executeQuery();
while (rs.next()) {
//insert code to validate
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeDBConnection() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Java + Sql database data rollback is not working

In my application, I am writing data to a database.
This is the code for database writing.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseRollback {
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("PostgreSQL JDBC Driver Registered!");
try {
dbConnection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/MyDB","root","123");
if (dbConnection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
return dbConnection;
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
System.out.println(e.getMessage());
e.printStackTrace();
}
return dbConnection;
}
public static void main(String args[]) throws SQLException {
Connection dbConnection = null;
PreparedStatement pstmt = null;
String query = "insert into orders(orderid, execid, exectype, lastprice, lastqty, ordstatus) values(?, ?, ?, ?, ?, ?)";
try {
dbConnection = getDBConnection();
dbConnection.setAutoCommit(false);
pstmt = dbConnection.prepareStatement(query); // create a statement
pstmt.setString(1, "OrderID");
pstmt.setString(2, "Test02");
pstmt.setString(3, "exectype");
pstmt.setDouble(4, 100.00);
pstmt.setInt(5, 100);
pstmt.setString(6,"ordstatus");
pstmt.executeUpdate();
dbConnection.commit();
query.concat("error");
dbConnection.prepareStatement(query);
pstmt.setString(1, "eeeeeeeeeeeeeeeeeeeeeeee");
pstmt.executeUpdate(); // here error comes.........
} catch (Exception e) {
// e.printStackTrace();
dbConnection.rollback();
}
finally {
if (pstmt != null) {
pstmt.close();
}
if (dbConnection != null && !dbConnection.isClosed()) {
dbConnection.close();
}
}
}}
After debug point reach to the dbConnection.commit(); I have checked database and one record was inserted. But then it goes through dbConnection.rollback(); section. but it doesn't rollback inserted record. How can I implement rollback machanism?
dbConnection.commit();
should be placed at the end of your transaction.
pstmt.setString(1, "eeeeeeeeeeeeeeeeeeeeeeee");
pstmt.executeUpdate(); // here error comes.........
dbConnection.commit();
} catch (Exception e) {

Liquibase does not commit my changes

I'm running Liquibase from a Java application to MSSQL with the JTDS driver. When I run the updates I see them displayed but nothing actually get's committed to the Database. Any ideas? The code below runs in a Servlet.
Connection con = null;
try {
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/datasource.properties"));
String driver = props.getProperty("database.driver");
String url = props.getProperty("database.url");
String username = props.getProperty("database.username");
String password = props.getProperty("database.password");
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(con));
Liquibase liquibase = new Liquibase("db.xml", new ClassLoaderResourceAccessor(), database);
response.getWriter().print("<PRE>");
liquibase.update("", response.getWriter());
con.commit();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServletException(e.getMessage(), e);
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.warn(e.getMessage(), e);
}
}
}
response.flushBuffer();
The update() method that takes a writer will not execute changes, but rather output what would be ran.
If you call liquibase.update("") or liquibase.update(null) instead, it will execute the changes.