summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PoolingDataSource.java
blob: df3e50ad074cdea057cc19c4d601335c4a224c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.postgresql.jdbc3;

import java.util.*;
import org.postgresql.jdbc2.optional.PoolingDataSource;
import org.postgresql.jdbc2.optional.ConnectionPool;

import javax.naming.Reference;

/**
 * JDBC 3 implementation of a pooling DataSource.  This is best
 * used outside of an application server environment.  Application
 * servers generally prefer to deal with instances of
 * ConnectionPoolDataSource (see ConnectionPool) or XADataSource
 * (not available for PostgreSQL).
 *
 * @author Aaron Mulder (ammulder@alumni.princeton.edu)
 * @version $Revision: 1.1 $
 */
public class Jdbc3PoolingDataSource extends PoolingDataSource
{
    /**
     * Store JDBC3 DataSources in different bucket than JDBC2 DataSources
     */
    private static Map dataSources = new HashMap();

    /**
     * Store JDBC3 DataSources in different bucket than JDBC2 DataSources
     */
    static Jdbc3PoolingDataSource getDataSource(String name)
    {
        return (Jdbc3PoolingDataSource) dataSources.get(name);
    }

    /**
     * Store JDBC3 DataSources in different bucket than JDBC2 DataSources
     */
    protected void removeStoredDataSource()
    {
        synchronized (dataSources)
        {
            dataSources.remove(dataSourceName);
        }
    }

    /**
     * Store JDBC3 DataSources in different bucket than JDBC2 DataSources
     */
    public void setDataSourceName(String dataSourceName)
    {
        if (isInitialized())
        {
            throw new IllegalStateException("Cannot set Data Source properties after DataSource has been used");
        }
        if (this.dataSourceName != null && dataSourceName != null && dataSourceName.equals(this.dataSourceName))
        {
            return;
        }
        synchronized (dataSources)
        {
            if (getDataSource(dataSourceName) != null)
            {
                throw new IllegalArgumentException("DataSource with name '" + dataSourceName + "' already exists!");
            }
            if (this.dataSourceName != null)
            {
                dataSources.remove(this.dataSourceName);
            }
            this.dataSourceName = dataSourceName;
            dataSources.put(dataSourceName, this);
        }
    }

    /**
     * Generates a JDBC3 object factory reference.
     */
    protected Reference createReference()
    {
        return new Reference(getClass().getName(), Jdbc3ObjectFactory.class.getName(), null);
    }

    /**
     * Creates a JDBC3 ConnectionPool to use with this DataSource.
     */
    protected ConnectionPool createConnectionPool()
    {
        return new Jdbc3ConnectionPool();
    }

    /**
     * Gets a description of this DataSource.
     */
    public String getDescription()
    {
        return "JDBC3 Pooling DataSource from " + org.postgresql.Driver.getVersion();
    }
}