use of edu.princeton.cs.algs4.Transaction in project algorithms-sedgewick-wayne by reneargento.
the class Exercise33_RandomTransactions method generateRandomTransactions.
private Exercise21_ComparableTransactions[] generateRandomTransactions(int numberOfObjects) {
Exercise21_ComparableTransactions[] transactions = new Exercise21_ComparableTransactions[numberOfObjects];
for (int i = 0; i < numberOfObjects; i++) {
String who = "Client " + (i + 1);
int month = StdRandom.uniform(12) + 1;
int maxDay = month == 2 ? 28 : 30;
int day = StdRandom.uniform(maxDay) + 1;
int year = StdRandom.uniform(1900, 2018);
Date date = new Date(month, day, year);
double amount = (double) Math.round(StdRandom.uniform(0.0, 1000000.0) * 100) / 100;
Exercise21_ComparableTransactions transaction = new Exercise21_ComparableTransactions(who, date, amount);
transactions[i] = transaction;
}
return transactions;
}
use of edu.princeton.cs.algs4.Transaction in project AlgorithmsSolutions by Allenskoo856.
the class TopM method main.
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
MinPQ<Transaction> pq = new MinPQ<Transaction>(M + 1);
while (StdIn.hasNextLine()) {
pq.insert(new Transaction(StdIn.readLine()));
if (pq.size() > M) {
pq.delMin();
}
}
Stack<Transaction> stack = new Stack<Transaction>();
while (!pq.isEmpty()) {
stack.push(pq.delMin());
}
for (Transaction t : stack) {
StdOut.println(t);
}
}
use of edu.princeton.cs.algs4.Transaction in project algorithms-sedgewick-wayne by reneargento.
the class Exercise25_HashCache method main.
public static void main(String[] args) {
Exercise25_HashCache hashCache = new Exercise25_HashCache();
Transaction transaction = hashCache.new Transaction("Person 1", new Date(1, 10, 5), 1000);
StdOut.println(transaction.hashCode() + " Expected: Cache miss");
StdOut.println(transaction.hashCode() + " Expected: Cache hit");
StdOut.println(transaction.hashCode() + " Expected: Cache hit");
}
use of edu.princeton.cs.algs4.Transaction in project wildfly-swarm by wildfly-swarm.
the class BMTStatefulTestBean method twoTransactions.
public String twoTransactions() throws Exception {
// start the JTA transaction via javax.transaction.UserTransaction
userTransaction.begin();
try {
// obtain session which will be enlisted into the JTA transaction.
Session session = driver.session();
if (session != driver.session()) {
throw new RuntimeException("multiple calls to Driver.session() must return the same session within JTA transaction.");
}
// Calls to Session.beginTransaction() in a JTA transaction are expected to throw a RuntimeException
try {
Transaction transaction = session.beginTransaction();
fail("Calling Session.beginTransaction in a JTA transaction should throw a RuntimeException.");
} catch (RuntimeException expectedException) {
// success
}
session.run("CREATE (a:Person {name:'BMT', title:'King'})");
// calls to close the session should also be ignored, since the session is also considered to be enlisted into the JTA transaction
session.close();
if (session.isOpen() != true) {
throw new RuntimeException("Session should be open since JTA transaction is still active.");
}
// commit the JTA transaction, which also calls org.neo4j.driver.v1.Transaction.success()/close().
// if the JTA transaction rolls back, org.neo4j.driver.v1.Transaction.failure()/close() would instead be called.
userTransaction.commit();
if (session.isOpen() != false) {
throw new RuntimeException("Session should now be closed since JTA transaction ended.");
}
// should be ignored
session.close();
// Start another JTA transaction, note that the session has to be obtained again
userTransaction.begin();
session = driver.session();
if (session != driver.session()) {
throw new RuntimeException("multiple calls to Driver.session() must return the same session within JTA transaction.");
}
// Calls to Session.beginTransaction() in a JTA transaction are expected to throw a RuntimeException
try {
Transaction transaction = session.beginTransaction();
fail("Calling Session.beginTransaction in a JTA transaction should throw a RuntimeException.");
} catch (RuntimeException expectedException) {
// success
}
StatementResult result = session.run("MATCH (a:Person) WHERE a.name = 'BMT' RETURN a.name AS name, a.title AS title");
Record record = result.next();
return record.toString();
} finally {
if (userTransaction.getStatus() == Status.STATUS_ACTIVE) {
// second JTA transaction is ended, which also closes the enlisted org.neo4j.driver.v1.Transaction/Session
userTransaction.commit();
}
Session cleanupSession = driver.session();
cleanupSession.run("MATCH (a:Person) delete a");
cleanupSession.close();
}
}
use of edu.princeton.cs.algs4.Transaction in project wildfly-swarm by wildfly-swarm.
the class StatefulTestBean method transactionEnlistmentReadAfterCallingTransactionClose.
public String transactionEnlistmentReadAfterCallingTransactionClose() {
// JTA transaction is started by CMT, the following obtains a Session that is enlisted into the JTA transaction.
Session session = injectedDriver.session();
// Calls to Session.beginTransaction() in a JTA transaction are expected to throw a RuntimeException
try {
Transaction transaction = session.beginTransaction();
fail("Calling Session.beginTransaction in a JTA transaction should throw a RuntimeException.");
} catch (RuntimeException expectedException) {
// success
}
try {
session.run("CREATE (a:Person {name:'TRANSACTION', title:'King'})");
return nestedBean.getPerson("TRANSACTION");
} finally {
if (session.isOpen()) {
// this will be true
session.run("MATCH (a:Person) delete a");
// ignored, session is auto closed when the transaction ends.
session.close();
}
}
}
Aggregations