Search in sources :

Example 1 with SimpleSessionState

use of io.jans.orm.cloud.spanner.model.SimpleSessionState in project jans by JanssenProject.

the class SpannerConcurentSessionUpdateSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    final SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    try {
        String sessionId = UUID.randomUUID().toString();
        final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
        final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=jans";
        final SimpleSessionState simpleSessionState = new SimpleSessionState();
        simpleSessionState.setDn(sessionDn);
        simpleSessionState.setId(sessionId);
        simpleSessionState.setLastUsedAt(new Date());
        sqlEntryManager.persist(simpleSessionState);
        System.out.println("Persisted");
        int threadCount = 500;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
        for (int i = 0; i < threadCount; i++) {
            final int count = i;
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    final SimpleSessionState simpleSessionStateFromSQL = sqlEntryManager.find(SimpleSessionState.class, sessionDn);
                    String beforeUserDn = simpleSessionStateFromSQL.getUserDn();
                    String randomUserDn = count % 2 == 0 ? userDn : "";
                    try {
                        simpleSessionStateFromSQL.setUserDn(randomUserDn);
                        simpleSessionStateFromSQL.setLastUsedAt(new Date());
                        sqlEntryManager.merge(simpleSessionStateFromSQL);
                        System.out.println("Merged thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn);
                    } catch (Throwable e) {
                        System.out.println("ERROR !!!, thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn + ", error:" + e.getMessage());
                    // e.printStackTrace();
                    }
                }
            });
        }
        Thread.sleep(5000L);
    } finally {
        sqlEntryManager.destroy();
    }
}
Also used : SimpleSessionState(io.jans.orm.cloud.spanner.model.SimpleSessionState) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) ExecutorService(java.util.concurrent.ExecutorService) Date(java.util.Date) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Example 2 with SimpleSessionState

use of io.jans.orm.cloud.spanner.model.SimpleSessionState in project jans by JanssenProject.

the class SpannerUpateMissingEntrySample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    String sessionId = UUID.randomUUID().toString();
    final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
    final SimpleSessionState simpleSessionState = new SimpleSessionState();
    simpleSessionState.setDn(sessionDn);
    simpleSessionState.setId(sessionId);
    simpleSessionState.setLastUsedAt(new Date());
    try {
        sqlEntryManager.merge(simpleSessionState);
        System.out.println("Updated");
    } catch (EntryPersistenceException ex) {
        LOG.info("Failed to update, root case exception: {}", ex.getCause().getClass(), ex);
    }
}
Also used : SimpleSessionState(io.jans.orm.cloud.spanner.model.SimpleSessionState) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) Date(java.util.Date) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Aggregations

SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)2 SimpleSessionState (io.jans.orm.cloud.spanner.model.SimpleSessionState)2 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)2 Date (java.util.Date)2 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)1 ExecutorService (java.util.concurrent.ExecutorService)1