Re: Help Needed with Connection Pooling for Java Swing Based

From: "Scot P(dot) Floess" <floess(at)mindspring(dot)com>
To: Shanmugasundaram Doraisamy <shan(at)ceedees(dot)com>
Cc: postgresql-jdbc <pgsql-jdbc(at)postgresql(dot)org>, blind(at)xythos(dot)com, cube-soft(at)rogers(dot)com
Subject: Re: Help Needed with Connection Pooling for Java Swing Based
Date: 2003-03-27 12:09:12
Message-ID: 3E82E9E8.7060802@mindspring.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Shan:

Can't you simply "associate" the connection with the window and when it
closes, return it at the pool? You could have a helper class who acts
as a window listener and contains a connection. When the window closes,
the helper class will be called - at that time have the helper class
return the connection to the pool.

Scot

Shanmugasundaram Doraisamy wrote:

>Dear Derek, Scott & Barry,
> Thank you for your replies. But, when I give the system.exit call
>the whole application is closed. This is not what I want to do. I just
>want to close only that particular form/ window alone and return the
>connection to the pool. Please let me know how this can be achieved.
>Thanking you,
>
>Yours sincerely,
>
>Shan.
>
>On Thu, 2003-03-27 at 04:09, Scot P. Floess wrote:
>
>
>>Derek:
>>
>>I see what you mean.
>>
>>Regarding needing to call System.exit...my guess is the Swing/AWT
>>threads are user threads (as opposed to daemon) and that is why one must
>>call System.exit()...
>>
>>Scot
>>
>>Derek S wrote:
>>
>>
>>
>>>Pardon my inaccuracy. I should have said "Realizing" instead of
>>>instantiating. "Realizing" is the name that the AWT developers gave
>>>to the process (which is encapsulated in the AWT's component
>>>implementation) of actually allocating the system resources needed to
>>>display the GUI widgets. Typically, it happens when you call pack or
>>>setVisible(true) on a Window, Frame or Dialog. When a component is
>>>realized, it checks to see if the dispatcher thread is running, and if
>>>not, starts it. Depending on what you're using, it may also start
>>>other threads to support features like the Pluggable Look and Feel and
>>>so on. As far as I know, it's only possible to have one AWT Event
>>>Dispatch thread.
>>>
>>>Bringing us back to the original topic, Once the AWT threads are
>>>running, it's basically hit-or-miss whether the app will stop unless
>>>System.exit is explicitly called or you set the default close
>>>operation on your frame to EXIT_ON_CLOSE (which just calls System.exit
>>>for you), so this has nothing to do with JDBC or PostgreSQL's driver.
>>>
>>>Scot P. Floess wrote:
>>>
>>>
>>>
>>>>Derek:
>>>>
>>>>Are you sure instantiating a Swing component launches a new thread?
>>>>Simply instantiating 2 JButtons will launch 2 threads? I don't think
>>>>that is the case.
>>>>
>>>>Derek S wrote:
>>>>
>>>>
>>>>
>>>>>Instantiating any Swing component does in fact start a new thread -
>>>>>the AWT Event Dispatch thread. My experience with it has been that
>>>>>this thread quite often doesn't terminate the way you might expect
>>>>>when the main method ends. This is a well known issue that Swing
>>>>>and AWT programmers simply have to deal with and has absolutely
>>>>>nothing to do with JDBC. Generally speaking, all Swing/AWT based
>>>>>applications call System.exit when they are finished doing whatever
>>>>>it is they do in order to ensure that the AWT Event Dispatch thread
>>>>>shuts down properly.
>>>>>
>>>>>Barry Lind wrote:
>>>>>
>>>>>
>>>>>
>>>>>>I just ran your test case and everything seems to be working
>>>>>>correctly as far as I can tell. The program creates one and only
>>>>>>one connection to the database. I don't see multiple connections
>>>>>>created as you are reporting.
>>>>>>
>>>>>>I don't know anything about swing, but it is clear that the code
>>>>>>for swing you have below isn't correct. Because with this swing
>>>>>>call the program doesn't exit. It seems the swing code is starting
>>>>>>some other threads that are running and thus the main program
>>>>>>doesn't exit until those threads stop running (and I don't know
>>>>>>what swing calls are necessary to stop the swing threads from
>>>>>>running).
>>>>>>
>>>>>>So with the swing call in place I have to ctrl-C to stop the app
>>>>>>from running, but from the jdbc side of things everything seems to
>>>>>>be working correctly.
>>>>>>
>>>>>>Perhaps I don't understand the problem you are seeing?
>>>>>>
>>>>>>thanks,
>>>>>>--Barry
>>>>>>
>>>>>>
>>>>>>Shanmugasundaram Doraisamy wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>>Dear group,
>>>>>>>
>>>>>>>We are having some problems when using JDBC Connection Pool using
>>>>>>>Postgrsql Jdbc3PoolingDataSource.
>>>>>>>
>>>>>>>The Pool behaves fine if there is no Swing Component in the Program.
>>>>>>>If there is a swing Component ,then the connection seems to be not
>>>>>>>returning to the pool and calling for an additional instance of
>>>>>>>the class leeds to the creation of fresh Connection objects ,at
>>>>>>>times it even exceeds the maximum number allowed in the pool.
>>>>>>>If we go for System.exit(0);(Killing) then only the Connection is
>>>>>>>returned .
>>>>>>>
>>>>>>>But if we comment the swing component line (here in the attached
>>>>>>>Program a JOptionPane is Used) then the pool behaves fine.
>>>>>>>
>>>>>>>
>>>>>>>Herewith Iam attaching a Program which is behaving as detailed.
>>>>>>>
>>>>>>>Any Help is Welcome.
>>>>>>>
>>>>>>>##################################################################
>>>>>>>
>>>>>>>
>>>>>>>##################################################################
>>>>>>>
>>>>>>>/*
>>>>>>> * ConPool.java
>>>>>>> *
>>>>>>> * Created on 26 March 2003, 11:54
>>>>>>> */
>>>>>>>
>>>>>>>/**
>>>>>>> *
>>>>>>> * @author naks from vpsd,Erode
>>>>>>> * */
>>>>>>>import org.postgresql.jdbc3.*;
>>>>>>>import java.sql.*;
>>>>>>>
>>>>>>>public class ConPool {
>>>>>>> Jdbc3PoolingDataSource source = null;
>>>>>>> private Connection conn = null;
>>>>>>> /** Creates a new instance of ConPool */
>>>>>>> public ConPool() {
>>>>>>> }
>>>>>>> /** Setting the Pool which intializes the Pool to 2. */
>>>>>>> public void setPool() {
>>>>>>> // DataSource initialization
>>>>>>> System.out.println("setPool()");
>>>>>>> try {
>>>>>>> System.out.println("setting pool");
>>>>>>> // constructs a pool only when source is not assigned.
>>>>>>> // this will prevent problems if the same pool is set
>>>>>>>more than
>>>>>>> // one time
>>>>>>> if (source == null) {
>>>>>>> source = new Jdbc3PoolingDataSource();
>>>>>>> // DataSource configuration.
>>>>>>> source.setServerName("192.168.0.51");
>>>>>>> source.setDatabaseName("kec_test_pool");
>>>>>>> source.setUser("venbro");
>>>>>>> source.setPassword("venbro");
>>>>>>> source.setMaxConnections(2);
>>>>>>> } else {
>>>>>>> System.out.println("pool is set");
>>>>>>> return;
>>>>>>> }
>>>>>>> } catch (Exception loException) {
>>>>>>> // logger.logToFile(loException);
>>>>>>> loException.printStackTrace();
>>>>>>> }
>>>>>>>
>>>>>>> } //End of setPool method
>>>>>>> /**
>>>>>>> * Method inserts data to a table "test" which is like
>>>>>>> * create table test (param1 varchar(2),param2 varchar(2)) ;
>>>>>>> */
>>>>>>> public int insertDatas(){
>>>>>>> int count=-1;
>>>>>>> try{
>>>>>>> conn=source.getConnection();
>>>>>>>
>>>>>>> PreparedStatement st = conn.prepareStatement("INSERT INTO
>>>>>>>test values('P1','P2')");
>>>>>>> count = st.executeUpdate();
>>>>>>>
>>>>>>> st.close();
>>>>>>> conn.commit();
>>>>>>> conn.close();
>>>>>>> }
>>>>>>> catch (SQLException ex) {
>>>>>>> count=0;
>>>>>>> ex.printStackTrace();
>>>>>>> }
>>>>>>> return count;
>>>>>>> }//End of Insert Method
>>>>>>> public static void main(String args[]){
>>>>>>> ConPool cp=new ConPool();
>>>>>>> cp. setPool();
>>>>>>> int sucess_flag=cp.insertDatas();
>>>>>>> System.out.println("Sucess"+sucess_flag);
>>>>>>> /**
>>>>>>> * Upto this level the Connection pooling is working fine .
>>>>>>> * But if this Swing Component is added then it creates
>>>>>>>new Connection
>>>>>>> * which is even more than the intial connection of the
>>>>>>>pool..
>>>>>>> * We have to kill the application (System.exit(0))to
>>>>>>>remove the Connection
>>>>>>> ** But on commenting line :105 "Swingline " the Pool is
>>>>>>>working fine..
>>>>>>> */
>>>>>>>
>>>>>>>javax.swing.JOptionPane.showMessageDialog(null,"VENBRO-ERODE","MESSAGE",1);//
>>>>>>>Swing line 105
>>>>>>> }
>>>>>>>
>>>>>>>}//End of the class
>>>>>>>##########################################################################################
>>>>>>>
>>>>>>>###########################################################################################
>>>>>>>
>>>>>>>
>>>>>>>---------------------------(end of
>>>>>>>broadcast)---------------------------
>>>>>>>TIP 3: if posting/reading through Usenet, please send an appropriate
>>>>>>>subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
>>>>>>>message can get through to the mailing list cleanly
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>---------------------------(end of
>>>>>>broadcast)---------------------------
>>>>>>TIP 5: Have you checked our extensive FAQ?
>>>>>>
>>>>>>http://www.postgresql.org/docs/faqs/FAQ.html
>>>>>>
>>>>>>
>>>>>>
>>>>>---------------------------(end of
>>>>>broadcast)---------------------------
>>>>>TIP 3: if posting/reading through Usenet, please send an appropriate
>>>>>subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
>>>>>message can get through to the mailing list cleanly
>>>>>
>>>>>
>>>>>
>>>
>>>
>>--
>>Scot P. Floess - 27 Lake Royale - Louisburg, NC 27549 - 252-478-8087
>>
>>Open Source Home Page
>>--------------------------------------
>>http://javapim.sourceforge.net
>>http://jplate.sourceforge.net
>>http://jwaretechniques.sourceforge.net
>>
>>Open Source Project Host
>>-----------------------------------------------
>>http://sourceforge.net/projects/javapim
>>http://sourceforge.net/projects/jplate
>>http://sourceforge.net/projects/jwaretechniques
>>
>>
>>
>>
>
>
>
>
>

--
Scot P. Floess - 27 Lake Royale - Louisburg, NC 27549 - 252-478-8087

Open Source Home Page
--------------------------------------
http://javapim.sourceforge.net
http://jplate.sourceforge.net
http://jwaretechniques.sourceforge.net

Open Source Project Host
-----------------------------------------------
http://sourceforge.net/projects/javapim
http://sourceforge.net/projects/jplate
http://sourceforge.net/projects/jwaretechniques

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Haig Ehramdjian 2003-03-27 15:10:08 Re: JDBC2 driver failure
Previous Message Shanmugasundaram Doraisamy 2003-03-27 06:47:33 Re: Help Needed with Connection Pooling for Java Swing Based