⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idbroker.java

📁 torque服务器源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param tableName a String that identifies a table.     * @return A long with the value for the id.     * @exception Exception Database error.     */    public long getIdAsLong(Connection connection, Object tableName)        throws Exception    {        return getIdAsBigDecimal(connection, tableName).longValue();    }    /**     * Returns an id as a BigDecimal. Note this method does not     * require a Connection, it just implements the KeyGenerator     * interface.  if a Connection is needed one will be requested.     * To force the use of the passed in connection set the configuration     * property torque.idbroker.usenewconnection = false     *     * @param connection A Connection.     * @param tableName a String that identifies a table..     * @return A BigDecimal id.     * @exception Exception Database error.     */    public BigDecimal getIdAsBigDecimal(Connection connection,                                        Object tableName)        throws Exception    {        BigDecimal[] id = getNextIds((String) tableName, 1, connection);        return id[0];    }    /**     * Returns an id as a String. Note this method does not     * require a Connection, it just implements the KeyGenerator     * interface.  if a Connection is needed one will be requested.     * To force the use of the passed in connection set the configuration     * property torque.idbroker.usenewconnection = false     *     * @param connection A Connection should be null.     * @param tableName a String that identifies a table.     * @return A String id     * @exception Exception Database error.     */    public String getIdAsString(Connection connection, Object tableName)        throws Exception    {        return getIdAsBigDecimal(connection, tableName).toString();    }    /**     * A flag to determine the timing of the id generation     *     * @return a <code>boolean</code> value     */    public boolean isPriorToInsert()    {        return true;    }    /**     * A flag to determine the timing of the id generation     *     * @return a <code>boolean</code> value     */    public boolean isPostInsert()    {        return false;    }    /**     * A flag to determine whether a Connection is required to     * generate an id.     *     * @return a <code>boolean</code> value     */    public boolean isConnectionRequired()    {        return false;    }    /**     * This method returns x number of ids for the given table.     *     * @param tableName The name of the table for which we want an id.     * @param numOfIdsToReturn The desired number of ids.     * @return A BigDecimal.     * @exception Exception Database error.     */    public synchronized BigDecimal[] getNextIds(String tableName,                                                int numOfIdsToReturn)        throws Exception    {        return getNextIds(tableName, numOfIdsToReturn, null);    }    /**     * This method returns x number of ids for the given table.     * Note this method does not require a Connection.     * If a Connection is needed one will be requested.     * To force the use of the passed in connection set the configuration     * property torque.idbroker.usenewconnection = false     *     * @param tableName The name of the table for which we want an id.     * @param numOfIdsToReturn The desired number of ids.     * @param connection A Connection.     * @return A BigDecimal.     * @exception Exception Database error.     */    public synchronized BigDecimal[] getNextIds(String tableName,                                                int numOfIdsToReturn,                                                Connection connection)        throws Exception    {        if (tableName == null)        {            throw new Exception("getNextIds(): tableName == null");        }        // A note about the synchronization:  I (jmcnally) looked at        // the synchronized blocks to avoid thread issues that were        // being used in this and the storeId method.  I do not think        // they were being effective, so I synchronized the method.        // I have left the blocks that did exist commented in the code        // to make it easier for others to take a look, because it        // would be preferrable to avoid the synchronization on the        // method        List availableIds = (List) ids.get(tableName);        if (availableIds == null || availableIds.size() < numOfIdsToReturn)        {            if (availableIds == null)            {                log.debug("Forced id retrieval - no available list");            }            else            {                log.debug("Forced id retrieval - " + availableIds.size());            }            storeIDs(tableName, true, connection);            availableIds = (List) ids.get(tableName);        }        int size = availableIds.size() < numOfIdsToReturn                ? availableIds.size() : numOfIdsToReturn;        BigDecimal[] results = new BigDecimal[size];        // We assume that availableIds will always come from the ids        // Hashtable and would therefore always be the same object for        // a specific table.        //        synchronized (availableIds)        //        {        for (int i = size - 1; i >= 0; i--)        {            results[i] = (BigDecimal) availableIds.get(i);            availableIds.remove(i);        }        //        }        return results;    }    /**     * Describe <code>exists</code> method here.     *     * @param tableName a <code>String</code> value that is used to identify     * the row     * @return a <code>boolean</code> value     * @exception TorqueException if an error occurs     * @exception Exception a generic exception.     */    public boolean exists(String tableName)        throws TorqueException, Exception    {        String query = new StringBuffer(100)            .append("select ")            .append(TABLE_NAME)            .append(" where ")            .append(TABLE_NAME).append("='").append(tableName).append('\'')            .toString();        boolean exists = false;        Connection dbCon = null;        try        {            String databaseName = tableMap.getDatabaseMap().getName();            dbCon = Torque.getConnection(databaseName);            Statement statement = dbCon.createStatement();            ResultSet rs = statement.executeQuery(query);            exists = rs.next();            statement.close();        }        finally        {            // Return the connection to the pool.            try            {                dbCon.close();            }            catch (Exception e)            {                log.error("Release of connection failed.", e);            }        }        return exists;    }    /**     * A background thread that tries to ensure that when someone asks     * for ids, that there are already some loaded and that the     * database is not accessed.     */    public void run()    {        log.debug("IDBroker thread was started.");        Thread thisThread = Thread.currentThread();        while (houseKeeperThread == thisThread)        {            try            {                houseKeeperThread.sleep(SLEEP_PERIOD);            }            catch (InterruptedException exc)            {                // ignored            }            // logger.info("IDBroker thread checking for more keys.");            Iterator it = ids.keySet().iterator();            while (it.hasNext())            {                String tableName = (String) it.next();                if (log.isDebugEnabled())                {                    log.debug("IDBroker thread checking for more keys "                            + "on table: " + tableName);                }                List availableIds = (List) ids.get(tableName);                int quantity = getQuantity(tableName, null).intValue();                if (quantity > availableIds.size())                {                    try                    {                        // Second parameter is false because we don't                        // want the quantity to be adjusted for thread                        // calls.                        storeIDs(tableName, false, null);                        if (log.isDebugEnabled())                        {                            log.debug("Retrieved more ids for table: " + tableName);                        }                    }                    catch (Exception exc)                    {                        log.error("There was a problem getting new IDs "                                     + "for table: " + tableName, exc);                    }                }            }        }        log.debug("IDBroker thread finished.");    }    /**     * Shuts down the IDBroker thread.     *     * Calling this method stops the thread that was started for this     * instance of the IDBroker. This method should be called during     * MapBroker Service shutdown.     */    public void stop()    {        houseKeeperThread = null;    }    /**     * Check the frequency of retrieving new ids from the database.     * If the frequency is high then we increase the amount (i.e.     * quantity column) of ids retrieved on each access.  Tries to     * alter number of keys grabbed so that IDBroker retrieves a new     * set of ID's prior to their being needed.     *     * @param tableName The name of the table for which we want an id.     */    private void checkTiming(String tableName)    {        // Check if quantity changing is switched on.        // If prefetch is turned off, changing quantity does not make sense        if (!configuration.getBoolean(DB_IDBROKER_CLEVERQUANTITY, true)            || !configuration.getBoolean(DB_IDBROKER_PREFETCH, true))        {            return;        }        // Get the last id request for this table.        java.util.Date lastTime = (java.util.Date) lastQueryTime.get(tableName);        java.util.Date now = new java.util.Date();        if (lastTime != null)        {            long thenLong = lastTime.getTime();            long nowLong = now.getTime();            int timeLapse = (int) (nowLong - thenLong);            if (timeLapse < SLEEP_PERIOD && timeLapse > 0)            {                if (log.isDebugEnabled())                {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -