Postgres Issue with JDBC

Поиск
Список
Период
Сортировка
Искать

Postgres Issue with JDBC

От:
"John Kunchandy" <johnk@stylusinc.com>
Дата:
Hi All

I am a novice in using Postgres.I have been mainly using Postgres with Zope
installed on Mandrake Linux.PLEASE SPARE A LITTLE TIME TO READ MY MAIL since
it is a bit long.
I am currently having some Major difficulties in running my application
developed in java using Postgres as the back end.
I will explain the system I am using.The main application is devloped in
Java on Mandrake Linux8.2 

Re: Postgres Issue with JDBC

От:
Tony Grant <tony@tgds.net>
Дата:
On Mon, 2003-05-05 at 10:41, John Kunchandy wrote:

> Or if it is POstgres which somehow
> is holding connections persistently then how do I stop it.Thank s in
> Advancew for reading this whole letter .Please point me in

Bad JSP design - nothing to do with PostgreSQL.


<%
connection_name.close();
Connconnection_name.close();
%>

I show I do it

Cheers

Tony Grant
-- 
www.tgds.net Library management software toolkit, 
redhat linux on Sony Vaio C1XD, 
Dreamweaver MX with Tomcat and PostgreSQL

Re: Postgres Issue with JDBC

От:
"John Kunchandy" <johnk@stylusinc.com>
Дата:
Hi Tony

Thanks for your prompt Reply.Could you pease guide me in closing the
connection without affecting the functioning of my application elsewhere.
what should I do??I am giving the step by step procedure followed.please
mark against the step where I shoudl close the connection and how I shoudl
close the correct connection .
1)The JSP Page sends a request
2)The Control Servlet understands the Request retrives a Connection from the
Connection class and calls teh Particular Java Class and passes the
Connection Object as a parameter.
3)The Java Class executes the Queries and sends back the Result or ResultSet
to the Control Servlet
4) The Control Servlet now makes a call to the ConnectionManager to Free the
Connection .(but since the Result Set still has values Connection is not
freed).
5)The JSp Page retrieves the values from the Request Object and Displays the
values .

IN between these steps where can I close the connection and whne .If I am to
close the connection in a JSP page how will I sned the correct Connection
Object to the JSP page and how will I close it in the JSP Page.
Kindly help me out.
Thanks once again for your Prompt reply

Best Regards
John
----- Original Message -----
From: "Tony Grant"
To: "John Kunchandy"
Sent: Monday, May 05 2003 2:35 PM
Subject: Re: [JDBC] Postgres Issue with JDBC


: On Mon, 2003-05-05 at 10:41, John Kunchandy wrote:
:
: > Or if it is POstgres which somehow
: > is holding connections persistently then how do I stop it.Thank s in
: > Advancew for reading this whole letter .Please point me in
:
: Bad JSP design - nothing to do with PostgreSQL..
:
: 
: <%
: connection_name.close();
: Connconnection_name.close();
: %>
:
: I show I do it
:
: Cheers
:
: Tony Grant
: --
============================================================================
===
Hi All

I am a novice in using Postgres.I have been mainly using Postgres with Zope
installed on Mandrake Linux.PLEASE SPARE A LITTLE TIME TO READ MY MAIL since
it is a bit long.
I am currently having some Major difficulties in running my application
developed in java using Postgres as the back end.
I will explain the system I am using.The main application is devloped in
Java on Mandrake Linux8.2 

Re: Postgres Issue with JDBC

От:
Tony Grant <tony@tgds.net>
Дата:
On Mon, 2003-05-05 at 11:53, John Kunchandy wrote:
> Hi Tony
> 
> Thanks for your prompt Reply.Could you pease guide me in closing the
> connection without affecting the functioning of my application elsewhere.
> what should I do??I am giving the step by step procedure followed.please
> mark against the step where I shoudl close the connection and how I shoudl
> close the correct connection .
> 1)The JSP Page sends a request
> 2)The Control Servlet understands the Request retrives a Connection from the
> Connection class and calls teh Particular Java Class and passes the
> Connection Object as a parameter.
> 3)The Java Class executes the Queries and sends back the Result or ResultSet
> to the Control Servlet
> 4) The Control Servlet now makes a call to the ConnectionManager to Free the
> Connection .(but since the Result Set still has values Connection is not
> freed).
> 5)The JSp Page retrieves the values from the Request Object and Displays the
> values .

Just close it at the end of the page. If you don't each time you open
your page it will create a new occurrence of the connection.

Cheers

Tony Grant
-- 
www.tgds.net Library management software toolkit, 
redhat linux on Sony Vaio C1XD, 
Dreamweaver MX with Tomcat and PostgreSQL

Re: Postgres Issue with JDBC

От:
"Jeroen Habets" <Jeroen@twofoldmedia.com>
Дата:

Hi John,

I return object wrappers to the JSP. F.e. my Java class has a method:

public MyBean getMyBean() {
	// JDBC stuff to do SQL query, read out ResultSet and put values in MyBean
	myBean.setField(rs.getString(1));
	// close result set, statement and connection
	return myBean;
}

When Controller puts MyBean in request (setAttribute) In JSP page I use



Without any JDBC stuff.

Jeroen


Example Code
(Note: My ConnectionProvider implementation uses a JNDI DataSource but could
also be implemented using pooling or the good old class.forName and
DriverManager.getConnection)


public class UserBalanceProvider implements  TransTypeConstants {

    private static UserBalanceProvider singleton = new
UserBalanceProvider();
    private UserBalanceProvider() {
    }
    public static UserBalanceProvider getInstance() { return singleton; }


    private final static String BALANCE_TABLE_ALIAS = "bal";
    private final static String BALANCE_TABLE = "user_balance";
    private final static String BALANCE_COLUMN_PREFIX =
BALANCE_TABLE_ALIAS+".";
    private final static String BALANCE_COLUMNS =
BALANCE_COLUMN_PREFIX+"user_id,"
												 + BALANCE_COLUMN_PREFIX+"credits100,"
												 + BALANCE_COLUMN_PREFIX+"megapoints,"
												 + BALANCE_COLUMN_PREFIX+"requested_pay_out_credits100,"
												 + BALANCE_COLUMN_PREFIX+"confirmed_pay_out_credits100,"
												 + BALANCE_COLUMN_PREFIX+"completed_pay_out_credits100,"
												 + BALANCE_COLUMN_PREFIX+"games_played,"
												 + BALANCE_COLUMN_PREFIX+"last_trans_id,"
												 + BALANCE_COLUMN_PREFIX+"modify_date";

    private UserBalance fillUserBalance(ResultSet rs) throws SQLException {
        return new UserBalance(rs.getInt(1),			//  userId
        					   rs.getInt(2),			// credits100
        					   rs.getInt(3),			// megapoints
        					   rs.getInt(4),			// requested_pay_out_credits100
        					   rs.getInt(5),			// confirmed_pay_out_credits100
        					   rs.getInt(6),			// completed_pay_out_credits100
        					   rs.getInt(7),			// games_played
        					   rs.getInt(8),			// last_trans_id
        					   rs.getTimestamp(9)		// modify_date
        					  );
    }


    /**
     * returns current {@link UserBalance user balance} from database.
     * @param userId ID of balance owner.
     * @return UserBalance
     */
    public UserBalance getUserBalance(int userId) throws AccountingException
{
        UserBalance result = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
			con = ConnectionProvider.getReadOnlyConnection();

            ps = con.prepareStatement("SELECT "+BALANCE_COLUMNS+" FROM
"+BALANCE_TABLE+" "+BALANCE_TABLE_ALIAS+" WHERE
"+BALANCE_COLUMN_PREFIX+"user_id=?");

            ps.setInt(1, userId);

            rs = ps.executeQuery();

            if (rs.first()) {
                result = fillUserBalance(rs);
            }
            rs.close();
            rs = null;
            ps.close();
            ps = null;
            con.close();
            con = null;
        } catch (Exception e) {
            throw new
AccountingException("AccountingProvider.getUserBalance("+userId+"):
caught="+e, e);
        } finally {
			if (rs != null) {
                try {
					rs.close();
                } catch (SQLException rsCloseE) {  }
			}
			if (ps != null) {
                try {
					ps.close();
                } catch (SQLException psCloseE) {  }

			}
            if (con != null) {
                try {
	                con.close();
                } catch (SQLException conCloseE) {
                }
            }
        }
		return result;
	}

Re: Postgres Issue with JDBC

От:
Curt Sampson <cjs@cynic.net>
Дата:
On Mon, 5 May 2003, John Kunchandy wrote:

> 4) The Control Servlet now makes a call to the ConnectionManager to Free the
> Connection .(but since the Result Set still has values Connection is not
> freed).

That sounds to me like a problem with your pooling. Your pooling code
should return a pooled connection to a properly usable state when
Connection.close() is called, regardless of the current state of the
connection. If it can't do so, it should do a "real" close of the
connection.

Pooling code is very difficult to write. I suggest that unless you are a
real expert, you just use someone else's. Or, if it's a low-volume site,
you can get by without any pooling at all.

cjs
-- 
Curt Sampson     +81 90 7737 2974   http://www.netbsd.org
    Don't you know, in this new Dark Age, we're all light.  --XTC

FAQ