pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nico <nicohmail-postgresql@yahoo.com>
To: pgsql-jdbc@postgresql.org
Subject: only postgresqsl in connection pool
Date: Fri, 1 Apr 2005 03:16:46 +0200
Message-ID: <d2i7fs$1521$1@news.hub.org> (raw)

Hello,
I have a servlet that runs on a postgresql-database. Or rather: that's what 
I thought...
Let me start at the beginning. Originally, I made it in MySQL, but this 
didn't meet my requirements: the version installed on the server didn't 
support subqueries, so I switched to PostgreSQL. In the mean time, the 
database was created in MySQL and in PostgreSQL.
When I get a connection, it connected to PostgreSQL. However, since I 
connect to the database through a connectionpool - I think - I only get 
connections from MySQL.
So here is my question:
how do I tell a java servlet I want a PostgreSQL connection - and not a 
MySQL connection- from a connection pool? I changed the username & password 
in the code for obvious security reasons...

Kind regards,
Nico.

Here is my code for connecting to the database:
    protected final static String strDbdriver = "org.postgresql.Driver";
    protected static void connect(String dbdriver, String methodname)
        throws DriverNotFoundException, ConnectionException
    {
        try
        {
            Class.forName(dbdriver);
            if(dbname==null)
                throw new 
menus.ConnectionException(dbdriver,"connect","dbname is null.");
            if(dbname.equals(""))
                throw new menus.ConnectionException(dbdriver, "connect", 
"dbname has empty string.");
            if(dbname.equals("null"))
                dbname="databasename";
            int connattempts=0;
            while(connattempts<=5)
            {
                try
                {
                    if(pool==null)
                    {
                        javax.naming.Context env=(javax.naming.Context)
                            new 
javax.naming.InitialContext().lookup("java:comp/env");
                        pool=(DataSource)env.lookup("jdbc/"+dbname);
                    }
                    connection=pool.getConnection("username","password");
                    connattempts=8;
                }
                catch(NullPointerException fout)
                {
                    disconnect();
                    connattempts++;
                }
                catch(SQLException fout)
                {
                    disconnect();
                    connattempts++;
                }
            }
            if(connattempts!=8)
            {
                try
                {
                    connection = 
DriverManager.getConnection("jdbc:postgresql://localhost/" +
                        dbname + "?user=username&password=password");
                }
                catch(SQLException fout)
                {
                    throw new menus.ConnectionException(strDbdriver, 
methodname,"connection failed.");
                }
            }
            statementM1 = connection.createStatement();
            statementM2 = connection.createStatement();
            statementM3 = connection.createStatement();
            statementM4 = connection.createStatement();
            statementE1 = connection.createStatement();
            statementE2 = connection.createStatement();
            statementMeta = connection.createStatement();
        }
        catch(ClassNotFoundException exception)
        {
            throw new DriverNotFoundException(dbdriver,methodname);
        }
        catch(SQLException fout)
        {
            disconnect();
            throw new menus.ConnectionException(dbdriver,methodname,"cannot 
create statements");
        }
        catch(NamingException fout)
        {
            disconnect();
            throw new menus.ConnectionException(dbdriver, 
methodname,"namingexception: "+fout.getExplanation());
        }
    } 





Message-ID: <d2i7fs$1521$1@news.hub.org>
Permalink:  ../d2i7fs$1521$1@news.hub.org/
Also on:    postgresql.org/message-id/d2i7fs$1521$1@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: nicohmail-postgresql@yahoo.com
  Subject: Re: only postgresqsl in connection pool
  In-Reply-To: <d2i7fs$1521$1@news.hub.org>
  References: <d2i7fs$1521$1@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox