2014-06-17 9 views
3

(온라인에서이 문제를 이미 보았지만 검색 용어로 "100"에 의존하는 검색어는 분명히 유망하지 않습니다.이 질문이 이미 있으면 용서하십시오. 질문을 받았다.)앱 재시동 후 버클리 DB 기본 키 시퀀스

나는 DPL 기능을 사용하여 자바로 버클리 DB를 가지고 놀기 시작했다.

public class Client { 

    // primary key 
    @PrimaryKey(sequence="Client_ID") 
    private long id; 
[...] 
} 

내가 차 인덱스를 사용하여 몇 가지 샘플 엔티티를 추가 해요 :

clientById = store.getPrimaryIndex(Long.class, Client.class); 
clientById.put(client); 

난 처음 나는 다음과 같은 기본 키 순서가있는 클래스 "클라이언트"를 생성 응용 프로그램을 시작, 다 괜찮아. 시퀀스는 1에서 시작하여 1 씩 증가합니다. 다음 번에 응용 프로그램을 시작하면 4시에 계속 진행되는 대신 (101 개의 스테핑은 1) 계속됩니다 (3 개의 샘플 엔티티가 있음). 어떻게 든이 동작에 영향을 줄 수 있습니까? 진행중인 순서를 갖고 싶습니다.

편집 :

public class Store { 
    private File dbfile; 
    private Environment env; 
    private EntityStore store; 

    public Store() { 
     this(new File(System.getProperty("user.home"), "tmdb")); 
    } 

    public Store(File dbfile) { 
     this.dbfile = dbfile; 
     setup(); 
    } 


    public void setup() { 
     EnvironmentConfig envConfig = new EnvironmentConfig(); 
     envConfig.setAllowCreate(true); 
     envConfig.setTransactional(true); 
     env = new Environment(dbfile, envConfig); 

     StoreConfig storeConfig = new StoreConfig(); 
     storeConfig.setAllowCreate(true); 
     storeConfig.setTransactional(true); 
     store = new EntityStore(env, "TimeManagement", storeConfig); 

    } 

    public void shutdown() { 
     store.close(); 
     env.close(); 
    } 


    public EntityStore getStore() { 
     return store; 
    } 
} 

ClientAccessor은 다음과 같이 간다 :

public static void main(String[] args) { 
     test(); 
     test(); 

    } 

    public static void test() { 
     // create some test clients 
     Client c1 = new Client("Client 1", "Cli 1 street 1", null, "55411", "Bingen"); 
     Client c2 = new Client("Client 2", "Cli 1 street 2", null, "55411", "Bingen"); 
     Client c3 = new Client("Test Custoamer"); 

     // create database 
     Store store = new Store(); 
     ClientAccessor ca = new ClientAccessor(store.getStore()); 

     ca.put(c1); 
     ca.put(c2); 
     ca.put(c3); 

     List<Client> clients = ca.getAll(); 
     for (Client c : clients) { 
      System.out.println(c); 
      System.out.println("-------------------"); 
     } 

     store.shutdown(); 
    } 

스토어는 다음과 같습니다

public class ClientAccessor { 
    private EntityStore store; 

    // primary index 
    PrimaryIndex<Long, Client> clientById; 


    public ClientAccessor(EntityStore store) { 
     this.store = store; 
     if (store == null) 
      throw new IllegalArgumentException("EntityStore can't be null!"); 
     clientById = store.getPrimaryIndex(Long.class, Client.class); 
    } 

    public void put(Client c) { 
     clientById.put(c); 
    } 

    public List<Client> getAll() { 
     ArrayList<Client> clients = new ArrayList<Client>(); 
     EntityCursor<Client> cursor = clientById.entities(); 

     for (Client c : cursor) { 
      clients.add(c); 
     } 

     cursor.close(); 

     return clients; 
    } 


} 

그리고 클라이언트의 모습이 내 testcode이 입니다 this :

@Entity 
public class Client { 

    // primary key 
    @PrimaryKey(sequence="Client_ID") 
    private long id; 

    // secondary keys 
    @SecondaryKey(relate=Relationship.MANY_TO_ONE) 
    private String name; 
    @SecondaryKey(relate=Relationship.MANY_TO_ONE) 
    private String address1; 
    @SecondaryKey(relate=Relationship.MANY_TO_ONE) 
    private String address2; 
    @SecondaryKey(relate=Relationship.MANY_TO_ONE) 
    private String plz; 
    @SecondaryKey(relate=Relationship.MANY_TO_ONE) 
    private String city; 

    private Client(){} 

    // address is optional 
    public Client(String name) { 
     this(name, null, null, null, null); 
    } 

    public Client(String name, String address1, String address2, String plz, String city) { 
     this.setName(name); 
     this.setAddress1(address1); 
     this.setAddress2(address2); 
     this.setPlz(plz); 
     this.setCity(city); 
    } 

    @Override 
    public String toString() { 
     String str = ""; 
     str += id + "\n"; 
     str += name + "\n"; 
     str += (address1 != null && ! address1.isEmpty()) ? address1 + "\n" : ""; 
     str += (address2 != null && ! address2.isEmpty()) ? address2 + "\n" : ""; 
     str += (plz != null && ! plz.isEmpty()) ? plz + " " : ""; 
     str += (city != null &&! city.isEmpty()) ? city + "\n" : ""; 

     return str; 
    } 


    // getters and setters 

    public long getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddress1() { 
     return address1; 
    } 

    public void setAddress1(String address1) { 
     this.address1 = address1; 
    } 

    public String getAddress2() { 
     return address2; 
    } 

    public void setAddress2(String address2) { 
     this.address2 = address2; 
    } 

    public String getPlz() { 
     return plz; 
    } 

    public void setPlz(String plz) { 
     this.plz = plz; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 
} 
+0

이 처음 후에 BDB의 내용을 덤프 수 :이 설정을 변경하려면 Store.setup() 방법의 끝 부분에이 코드를 추가? 아마도 응용 프로그램에서 DB에 100 개의 레코드를 저장할 공간을 확보 한 다음 그 중 3 개만 채우는 것일 수도 있습니다. – dg99

+0

당신은'EntityStore'에서'close'를 제대로 호출하고 있습니까? 동기화 비용을 줄이기 위해 ID가 블록의 스레드에 할당 될 가능성이 큽니다. 'close'는 블록의 사용하지 않는 부분을 기록해야합니다. – Gene

+0

사실 EntityStore를 닫는 것을 잊어 버렸지 만 지금은 수정했습니다. 그럼에도 불구하고 그 행동은 계속됩니다. 필자는 테스트 코드를 전용 테스트 메서드로 옮기고 프로그램을 두 번 실행하는 것과 같은 결과를 사용하여 main() 메서드에서 두 번 호출했습니다. 내 질문에 해당 코드를 추가하겠습니다 ... – wullxz

답변

3

기본적으로 시퀀스는 100 개의 항목을 캐시합니다. 당신이 응용 프로그램을 실행

SequenceConfig sequenceConfig = store.getSequenceConfig("Client_ID"); 
    sequenceConfig.setCacheSize(1); 
    store.setSequenceConfig("Client_ID", sequenceConfig); 
+0

최신 버전 6.1.5에서 작동했지만 setSequenceConfig() 메소드를 이전 버전에서 사용할 수 없다는 점에 유의하십시오 5.0.73 –

+0

감사합니다! 그게 해결 됐어! 당신은 또한 모든 시퀀스에 대해 이것을 디폴트로 설정하는 방법을 알고 있습니까? – wullxz