Last active
March 30, 2017 18:51
-
-
Save stijnvanbael/a026d4a4d9a1c768e7c0b8a445d9531f to your computer and use it in GitHub Desktop.
Transaction demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mariadb.jdbc.MariaDbDataSource; | |
import javax.sql.DataSource; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Timestamp; | |
import java.util.Scanner; | |
public class TransactionDemo { | |
private static long interval = 1000; | |
private static boolean interrupted = false; | |
private static String connectionMethod = "single"; | |
public static void main(String[] args) throws InterruptedException { | |
if (args.length == 2) { | |
connectionMethod = args[0]; | |
interval = Long.parseLong(args[1]); | |
} | |
System.out.println("Starting transaction demo"); | |
System.out.println("Connection method: " + connectionMethod); | |
System.out.println("Interval: " + interval + "ms"); | |
System.out.println("Press [ENTER] to stop"); | |
new Thread(() -> { | |
try { | |
switch (connectionMethod) { | |
case "single": | |
singleConnection(); | |
break; | |
case "connection-pool": | |
connectionPool(); | |
break; | |
case "hibernate": | |
hibernate(); | |
break; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
}, "Transaction runner").start(); | |
Scanner scanner = new Scanner(System.in); | |
scanner.nextLine(); | |
System.out.println("Stopping ..."); | |
interrupted = true; | |
Thread.sleep(interval); | |
System.out.print("Bye!"); | |
} | |
private static void hibernate() { | |
// TODO | |
} | |
private static void connectionPool() { | |
// TODO | |
} | |
private static void singleConnection() throws Exception { | |
DataSource dataSource = createDataSource(); | |
Connection connection = dataSource.getConnection(); | |
connection.prepareStatement("BEGIN").execute(); | |
int sequence = 1; | |
while (!interrupted) { | |
System.out.println("Inserting record " + sequence); | |
PreparedStatement statement = connection.prepareStatement("INSERT INTO transaction_log(seq_no, date_time, connector) VALUES(?, ?, ?)"); | |
statement.setInt(1, sequence); | |
statement.setTimestamp(2, new Timestamp(System.currentTimeMillis())); | |
statement.setString(3, connectionMethod); | |
statement.execute(); | |
Thread.sleep(interval); | |
sequence++; | |
} | |
connection.prepareStatement("COMMIT").execute(); | |
System.out.println("Stopped"); | |
} | |
private static MariaDbDataSource createDataSource() throws SQLException { | |
MariaDbDataSource dataSource = new MariaDbDataSource("localhost", 3306, "transaction_demo"); | |
dataSource.setUserName("root"); | |
dataSource.setPassword("root"); | |
return dataSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment