protected void allocateBlock()

in tapestry-examples/VlibBeans/src/org/apache/tapestry/vlib/ejb/impl/KeyAllocatorBean.java [215:303]


    protected void allocateBlock(int count)
    {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet set = null;
        int nextKey;
        int allocationCount;
        int i;

        allocationCount = Math.max(count, blockSize);

        try
        {
            connection = getConnection();

            statement = connection.prepareStatement("select PROP_VALUE from PROP where NAME = ?");
            statement.setString(1, PROPERTY_NAME);

            set = statement.executeQuery();

            // Advance to the first row.

            set.next();

            nextKey = set.getInt(1);

            set.close();
            set = null;

            statement.close();
            statement = null;

            // Now, take those keys and advance nextKey

            for (i = 0; i < allocationCount; i++)
                keys.add(new Integer(nextKey++));

            // Update nextKey back to the database.

            statement =
                connection.prepareStatement("update PROP\n" + "set PROP_VALUE = ?\n" + "where NAME	 = ?");
            statement.setInt(1, nextKey);
            statement.setString(2, PROPERTY_NAME);

            statement.executeUpdate();
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();

            throw new XEJBException("Unable to allocate keys from the database.", ex);
        }
        finally
        {
            if (set != null)
            {
                try
                {
                    set.close();
                }
                catch (SQLException ex)
                {
                }
            }

            if (statement != null)
            {
                try
                {
                    statement.close();
                }
                catch (SQLException ex)
                {
                }
            }

            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (SQLException ex)
                {
                }
            }
        }

    }