1 package org.splat.kernel;
3 * Optimized generator of persistent identifiers.<br/>
4 * This generator minimizes the database hits by grouping a bunch of identifiers in memory and only hitting the database
5 * when the in-memory value group is exhausted.
6 * Only one IDPool object held by the Database class is created during a session.
8 * @author Daniel Brunier-Coulin
9 * @copyright OPEN CASCADE 2012
12 import java.sql.Connection;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
17 import org.hibernate.Session;
18 import org.hibernate.jdbc.Work;
24 private Integer lastid; // Last generated ID
25 private String version; // Version of the database schema
28 private int remaining; // Remaining available ID held in this pool
29 private static int poolsize = 1; // No pool by default
31 private class LoadNewIDs implements Work {
32 // -----------------------------------------
33 public void execute(Connection connex) throws SQLException
35 Statement request = connex.createStatement();
36 ResultSet result = request.executeQuery("SELECT MAX(rid) AS lastid FROM any");
37 StringBuffer command = new StringBuffer("UPDATE any SET rid=");
40 lastid = result.getInt("lastid");
42 command.append(lastid + poolsize).append(" WHERE rid=").append(lastid);
43 request.execute(command.toString());
48 private class LoadLastID implements Work {
49 // -----------------------------------------
50 public void execute(Connection connex) throws SQLException
52 Statement request = connex.createStatement();
53 ResultSet result = request.executeQuery("SELECT MAX(rid) AS lastid FROM any");
54 StringBuffer command = new StringBuffer("SELECT version FROM any WHERE rid=");
57 lastid = result.getInt("lastid");
59 command.append(lastid);
60 result = request.executeQuery(command.toString());
62 version = result.getString("version");
67 // ==============================================================================================================================
69 // ==============================================================================================================================
74 * Constructor called only once, when initializing the database.
76 * @param version the version of the constructed database schema.
78 protected IDPool (String version) {
79 // ---------------------------------
80 this.lastid = 0; //TODO: Get the current last ID if a previous version exists
81 this.version = version;
85 * Constructor called at start of every database session.
87 * @param base the started database session
89 protected IDPool (Session base) {
90 // -------------------------------
91 base.doWork( new LoadLastID() );
94 // ==============================================================================================================================
95 // Protected member functions
96 // ==============================================================================================================================
98 protected Integer getNextID () {
99 // ------------------------------
100 if (remaining <= 0) Database.getSession().doWork( new LoadNewIDs() );
106 protected String getSchemaVersion () {
107 // ------------------------------------
111 protected static void setPoolSize (int size) {
112 // --------------------------------------------
113 if (size > 1) poolsize = size;