Re: Help Needed with Connection Pooling for Java Swing Based - Mailing list pgsql-jdbc

From Derek S
Subject Re: Help Needed with Connection Pooling for Java Swing Based
Date
Msg-id 3E821F46.6010003@rogers.com
Whole thread Raw
In response to Re: Help Needed with Connection Pooling for Java Swing Based  (Barry Lind <blind@xythos.com>)
Responses Re: Help Needed with Connection Pooling for Java Swing Based
List pgsql-jdbc
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@postgresql.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
>


pgsql-jdbc by date:

Previous
From: Barry Lind
Date:
Subject: Re: Help Needed with Connection Pooling for Java Swing Based
Next
From: "Scot P. Floess"
Date:
Subject: Re: Help Needed with Connection Pooling for Java Swing Based