001package io.ebean.bean;
002
003import io.ebean.Database;
004
005import java.util.concurrent.locks.Lock;
006import java.util.concurrent.locks.ReentrantLock;
007
008/**
009 * BeanLoader used when single beans are loaded (which is usually not ideal / N+1).
010 */
011public abstract class SingleBeanLoader implements BeanLoader {
012
013  private final ReentrantLock lock = new ReentrantLock();
014
015  protected final Database database;
016
017  SingleBeanLoader(Database database) {
018    this.database = database;
019  }
020
021  @Override
022  public String getName() {
023    return database.getName();
024  }
025
026  @Override
027  public Lock lock() {
028    lock.lock();
029    return lock;
030  }
031
032  /**
033   * Single bean lazy loaded when bean from L2 cache.
034   */
035  public static class L2 extends SingleBeanLoader {
036    public L2(Database database) {
037      super(database);
038    }
039
040    @Override
041    public void loadBean(EntityBeanIntercept ebi) {
042      database.getPluginApi().loadBeanL2(ebi);
043    }
044  }
045
046  /**
047   * Single bean lazy loaded when a reference bean.
048   */
049  public static class Ref extends SingleBeanLoader {
050    public Ref(Database database) {
051      super(database);
052    }
053
054    @Override
055    public void loadBean(EntityBeanIntercept ebi) {
056      database.getPluginApi().loadBeanRef(ebi);
057    }
058  }
059
060  /**
061   * Single bean lazy loaded when a reference bean.
062   */
063  public static class Dflt extends SingleBeanLoader {
064    public Dflt(Database database) {
065      super(database);
066    }
067
068    @Override
069    public void loadBean(EntityBeanIntercept ebi) {
070      database.getPluginApi().loadBean(ebi);
071    }
072  }
073}