Search in sources :

Example 6 with Transaction

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;
}
Also used : Date(edu.princeton.cs.algs4.Date) Exercise21_ComparableTransactions(chapter2.section1.Exercise21_ComparableTransactions)

Example 7 with Transaction

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);
    }
}
Also used : Transaction(com.jimmysun.algorithms.chapter2_1.Transaction) MinPQ(edu.princeton.cs.algs4.MinPQ) Stack(com.jimmysun.algorithms.chapter1_3.Stack)

Example 8 with Transaction

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");
}
Also used : Date(edu.princeton.cs.algs4.Date)

Example 9 with Transaction

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();
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) UserTransaction(javax.transaction.UserTransaction) Transaction(org.neo4j.driver.v1.Transaction) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session)

Example 10 with Transaction

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();
        }
    }
}
Also used : Transaction(org.neo4j.driver.v1.Transaction) Session(org.neo4j.driver.v1.Session)

Aggregations

Test (org.junit.Test)8 Transaction (org.neo4j.driver.v1.Transaction)8 Session (org.neo4j.driver.v1.Session)7 Record (org.neo4j.driver.v1.Record)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)5 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)5 Driver (org.neo4j.driver.v1.Driver)5 Date (edu.princeton.cs.algs4.Date)4 Transaction (edu.princeton.cs.algs4.Transaction)4 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 Transaction (com.google.spanner.v1.Transaction)3 In (edu.princeton.cs.algs4.In)2 Queue (edu.princeton.cs.algs4.Queue)2 ReadReplica (org.neo4j.causalclustering.discovery.ReadReplica)2 Exercise21_ComparableTransactions (chapter2.section1.Exercise21_ComparableTransactions)1 Stack (com.jimmysun.algorithms.chapter1_3.Stack)1 Transaction (com.jimmysun.algorithms.chapter2_1.Transaction)1 MinPQ (edu.princeton.cs.algs4.MinPQ)1 HashSet (java.util.HashSet)1