Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory) 

Source Link

Document

Create a new GenericObjectPool using the specified factory.

Usage

From source file:ua.company.model.factory.PostgreDAOFactory.java

private void initSource(/*Logger logger*/) {

    // load the underlying driver
    try {//  ww w.  j a v  a2 s . c om
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException ex) {
        logger.error(ex.getMessage());
        System.exit(1);
    }

    ConnectionFactory connectionFactory = null;
    ObjectPool connectionPool = null;
    PoolableConnectionFactory poolableConnectionFactory = null;

    // Build the DSN: jdbc:postgresql://host:port/database
    String buf = "jdbc:postgresql://127.0.0.1:5432/Restaurant";

    Properties props = new Properties();
    props.setProperty("user", "postgres");
    props.setProperty("password", "postgres");
    props.setProperty("initialSize", "50");

    props.setProperty("maxActive", "25");

    connectionFactory = new DriverManagerConnectionFactory(buf, props);
    connectionPool = new GenericObjectPool(null);
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null,
            false, true);

    dataSource = new PoolingDataSource(connectionPool);
}

From source file:us.daveread.basicquery.BasicQuery.java

/**
 * Builds the GUI for the application//  ww w  . j a  v  a 2  s. c om
 */
private void setup() {
    JPanel panel;
    JPanel gridPanel;
    JPanel outerPanel;
    JPanel flowPanel;
    JPanel boxedPanel;
    ButtonGroup bGroup;
    MaxHeightJScrollPane maxHeightJScrollPane;

    setupComponents();

    getContentPane().setLayout(new BorderLayout());

    // table.getTableHeader().setFont(new Font(table.getTableHeader().getFont().
    // getName(), table.getTableHeader().getFont().getStyle(),
    // MessageStyleFactory.instance().getFontSize()));
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

    panel = new JPanel();
    panel.setLayout(new BorderLayout());

    outerPanel = new JPanel();
    outerPanel.setLayout(new BorderLayout());

    gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(0, 1));

    gridPanel.add(connectString = new JComboBox());
    connectString.setEditable(true);
    gridPanel.add(querySelection = new JComboBox());
    querySelection.setEditable(false);
    querySelection.addActionListener(this);
    outerPanel.add(gridPanel, BorderLayout.NORTH);

    outerPanel.add(new JScrollPane(queryText = new JTextArea(QUERY_AREA_ROWS, QUERY_AREA_COLUMNS)),
            BorderLayout.SOUTH);
    queryText.setLineWrap(true);
    queryText.setWrapStyleWord(true);
    queryText.addKeyListener(this);

    panel.add(outerPanel, BorderLayout.CENTER);

    outerPanel = new JPanel();
    outerPanel.setLayout(new BorderLayout());

    boxedPanel = new JPanel();
    boxedPanel.setLayout(new GridLayout(0, 2));
    boxedPanel.add(new JLabel(Resources.getString("proUserId")));
    boxedPanel.add(userId = new JTextField(10));
    boxedPanel.add(new JLabel(Resources.getString("proPassword")));
    boxedPanel.add(password = new JPasswordField(10));
    outerPanel.add(boxedPanel, BorderLayout.WEST);

    // Prev/Next and the checkboxes are all on the flowPanel - Center of
    // outerPanel
    flowPanel = new JPanel();
    flowPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

    // Previous/Next buttons
    boxedPanel = new JPanel();
    boxedPanel.setLayout(new FlowLayout());
    boxedPanel.add(previousQuery = new JButton(Resources.getString("ctlPrev"),
            new ImageIcon(ImageUtility.getImageAsByteArray("ArrowLeftGreen.gif"))));
    previousQuery.setToolTipText(Resources.getString("tipPrev"));
    previousQuery.addActionListener(this);
    boxedPanel.add(nextQuery = new JButton(Resources.getString("ctlNext"),
            new ImageIcon(ImageUtility.getImageAsByteArray("ArrowRightGreen.gif"))));
    nextQuery.setToolTipText(Resources.getString("tipNext"));
    nextQuery.addActionListener(this);
    flowPanel.add(boxedPanel);

    // Checkboxes: Autocommit, Read Only and Pooling
    boxedPanel = new JPanel();
    boxedPanel.setLayout(new FlowLayout());
    boxedPanel.setBorder(getStandardBorder());
    boxedPanel.add(autoCommit = new JCheckBox(Resources.getString("ctlAutoCommit"), true));
    boxedPanel.add(readOnly = new JCheckBox(Resources.getString("ctlReadOnly"), false));
    boxedPanel.add(poolConnect = new JCheckBox(Resources.getString("ctlConnPool"), false));
    poolConnect.setEnabled(false);
    flowPanel.add(boxedPanel);
    outerPanel.add(flowPanel, BorderLayout.CENTER);

    boxedPanel = new JPanel();
    boxedPanel.setLayout(new GridLayout(0, 1));
    boxedPanel.setBorder(getStandardBorder());
    boxedPanel.add(runIndicator = new JLabel(Resources.getString("ctlRunning"), JLabel.CENTER));
    runIndicator.setForeground(Color.lightGray);
    boxedPanel.add(timeIndicator = new JLabel("", JLabel.RIGHT));
    outerPanel.add(boxedPanel, BorderLayout.EAST);

    panel.add(outerPanel, BorderLayout.NORTH);

    flowPanel = new JPanel();
    flowPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    boxedPanel = new JPanel();
    boxedPanel.setLayout(new FlowLayout());
    boxedPanel.setBorder(getStandardBorder());
    boxedPanel.add(new JLabel(Resources.getString("proQueryType")));
    boxedPanel.add(asQuery = new JRadioButton(Resources.getString("ctlSelect"), true));
    boxedPanel.add(asUpdate = new JRadioButton(Resources.getString("ctlUpdate")));
    boxedPanel.add(asDescribe = new JRadioButton(Resources.getString("ctlDescribe")));
    bGroup = new ButtonGroup();
    bGroup.add(asQuery);
    bGroup.add(asUpdate);
    bGroup.add(asDescribe);
    asQuery.addActionListener(this);
    asUpdate.addActionListener(this);
    asDescribe.addActionListener(this);
    flowPanel.add(boxedPanel);

    flowPanel.add(new JLabel("     "));

    boxedPanel = new JPanel();
    boxedPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    boxedPanel.setBorder(getStandardBorder());
    boxedPanel.add(new JLabel(Resources.getString("proMaxRows")));
    boxedPanel.add(maxRows);
    flowPanel.add(boxedPanel);

    flowPanel.add(new JLabel("     "));

    flowPanel.add(execute = new JButton(Resources.getString("ctlExecute")));
    execute.addActionListener(this);
    flowPanel.add(remove = new JButton(Resources.getString("ctlRemove")));
    remove.addActionListener(this);
    flowPanel.add(commentToggle = new JButton(Resources.getString("ctlComment")));
    commentToggle.addActionListener(this);
    flowPanel.add(nextInList = new JButton(Resources.getString("ctlDown")));
    nextInList.addActionListener(this);

    panel.add(flowPanel, BorderLayout.SOUTH);

    getContentPane().add(panel, BorderLayout.NORTH);
    getRootPane().setDefaultButton(execute);

    messageDocument = new DefaultStyledDocument();
    getContentPane().add(
            maxHeightJScrollPane = new MaxHeightJScrollPane(message = new JTextPane(messageDocument)),
            BorderLayout.SOUTH);
    message.setEditable(false);

    loadedDBDriver = false;

    loadMenu();

    setupTextStyles();
    loadProperties();
    setupUserDefinedColoring();
    setupResultsTableColoring();
    loadConfig();
    loadConnectStrings();
    loadQueries();

    loadDrivers();

    // Check for avail of pool - enable/disable pooling option as appropriate
    // Not really useful until we get the pooling classes out of this code
    try {
        new GenericObjectPool(null);
        poolConnect.setEnabled(true);
        poolConnect.setSelected(true);
    } catch (Throwable any) {
        // No Apache Commons DB Pooling Library Found (DBCP)
        LOGGER.error(Resources.getString("errNoPoolLib"), any);
    }

    setDefaults();

    maxHeightJScrollPane.lockHeight(getHeight() / MAX_SCROLL_PANE_DIVISOR_FOR_MAX_HEIGHT);

    // Font
    setFontFromConfig(Configuration.instance());

    setVisible(true);
}

From source file:us.daveread.basicquery.BasicQuery.java

/**
 * Sets the database connection pool./*from  w ww .ja v  a 2s .c o m*/
 * 
 * @param connectURI
 *          The url specifying the database to which it connects
 * @param pUserId
 *          The user id field
 * @param pPassword
 *          The password field
 */
private void setupDBPool(String connectURI, String pUserId, String pPassword) {
    removeDBPool();

    try {
        final GenericObjectPool connectionPool = new GenericObjectPool(null);

        configurePool(connectionPool, connectURI, pUserId, pPassword);

        final PoolingDriver driver = new PoolingDriver();
        driver.registerPool(DBPOOL_NAME, connectionPool);
        LOGGER.info("DB Connection Pool setup [" + DBPOOL_NAME + "]");
    } catch (Throwable any) {
        LOGGER.error("Unable to setup database connection pool", any);
        messageOut(Resources.getString("errFailSetupDBPool", any.getMessage()), STYLE_RED);
    }
}