예제 #1
0
  /**
   * Creates a new {@link EntityDataStore} with the given configuration.
   *
   * @param configuration to use
   */
  public EntityDataStore(Configuration configuration) {
    closed = new AtomicBoolean();
    readers = new ClassMap<>();
    writers = new ClassMap<>();
    entityModel = Objects.requireNotNull(configuration.getModel());
    connectionProvider = Objects.requireNotNull(configuration.getConnectionProvider());
    mapping = configuration.getMapping();
    platform = configuration.getPlatform();
    transactionMode = configuration.getTransactionMode();
    this.configuration = configuration;
    statementListeners = new CompositeStatementListener(configuration.getStatementListeners());
    stateListeners = new CompositeEntityListener<>();

    entityCache =
        configuration.getCache() == null ? new EmptyEntityCache() : configuration.getCache();
    int statementCacheSize = configuration.getStatementCacheSize();
    if (statementCacheSize > 0) {
      statementCache = new PreparedStatementCache(statementCacheSize);
    }
    // set default mapping (otherwise deferred to getConnection()
    if (platform != null && mapping == null) {
      mapping = new GenericMapping(platform);
    }
    context = new DataContext();
    transactionProvider = new TransactionProvider(context);
    updateOperation = new UpdateOperation(context);
    countOperation = new SelectCountOperation(context);
    Set<EntityStateListener<T>> entityListeners = new LinkedHashSet<>();
    if (configuration.getUseDefaultLogging()) {
      LoggingListener<T> logListener = new LoggingListener<>();
      entityListeners.add(logListener);
      statementListeners.add(logListener);
    }
    if (!configuration.getEntityStateListeners().isEmpty()) {
      for (@SuppressWarnings("unchecked")
      EntityStateListener<T> listener : configuration.getEntityStateListeners()) {
        entityListeners.add(listener);
      }
    }
    if (!entityListeners.isEmpty()) {
      stateListeners.enableStateListeners(true);
      for (EntityStateListener<T> listener : entityListeners) {
        stateListeners.addPostLoadListener(listener);
        stateListeners.addPostInsertListener(listener);
        stateListeners.addPostDeleteListener(listener);
        stateListeners.addPostUpdateListener(listener);
        stateListeners.addPreInsertListener(listener);
        stateListeners.addPreDeleteListener(listener);
        stateListeners.addPreUpdateListener(listener);
      }
    }
  }
예제 #2
0
 @Override
 public <E extends T> Selection<? extends Scalar<Integer>> count(Class<E> type) {
   checkClosed();
   Objects.requireNotNull(type);
   return new QueryElement<>(SELECT, entityModel, countOperation)
       .select(Count.count(type))
       .from(type);
 }
예제 #3
0
 @Override
 public <V> V runInTransaction(Callable<V> callable, @Nullable TransactionIsolation isolation) {
   Objects.requireNotNull(callable);
   checkClosed();
   Transaction transaction = transactionProvider.get();
   if (transaction == null) {
     throw new TransactionException("no transaction");
   }
   try {
     transaction.begin(isolation);
     V result = callable.call();
     transaction.commit();
     return result;
   } catch (Exception e) {
     throw new RollbackException(e);
   }
 }