[postgis-users] Persistente of Jdbc Connection

Noredine Berrached nberrached at yahoo.com
Thu Apr 13 02:44:43 PDT 2006


Hi everyone,
I've got a problem with my application (on Fedora core
4, postgresql serveur 8.0.7, postgis 1.1.1), I launch
a periodic task with the Java Object TimerTask every
two seconds, to change the style of the visible
objects of the visible layer. The color change to
green to red, and the next time red to green.
The problem is that at each change of style, my
application open one or more a connection to the
server and never close it, then after a while a java
exception appears "too many clients connected".
How can I manage the connections?
this the code that I use to connect to the server, I
store the layer in a Vector:
//the connection parameters->
String database;
String namespace =(String)params.get("namespace");
String host = (String)params.get("host");
int port = ((Integer)params.get("port")).intValue();
String dbname = (String)params.get("database");
String schema = (String)params.get("schema");
String user = (String)params.get("user");
String password =(String)params.get("password");
factory = new PostgisConnectionFactory(host, port,
dbname);
JDBCDataStoreConfig config =
JDBCDataStoreConfig.createWithNameSpaceAndSchemaName(
namespace, schema );        

//new connectionPool->
pool = factory.getConnectionPool(user, password);
//new DataStore->
pgDatastore = new PostgisDataStore(pool, config,
PostgisDataStore.OPTIMIZE_SAFE);
m_vect_feature_source = new Vector();
FeatureSource fsRoads =
pgDatastore.getFeatureSource("roads");
FeatureSource fsStreams =
pgDatastore.getFeatureSource("streams");
FeatureSource fsBugs =
pgDatastore.getFeatureSource("bugsites");
FeatureSource fsArch =
pgDatastore.getFeatureSource("archsites");
FeatureSource fsRestricted =
pgDatastore.getFeatureSource("rstrct");

m_vect_feature_source.add(fsRoads);
m_vect_feature_source.add(fsStreams);
m_vect_feature_source.add(fsBugs);
m_vect_feature_source.add(fsArch);
m_vect_feature_source.add(fsRestricted);

Here is the code that the periodic task uses :
//first I store the visible object in a FeatureResults
with a Filter
...(too long)
//storage of the identifier in a Vector, and swap of
the color ->
FeatureResults fr = fsRestricted.getFeatures(filt);
if (fr.getCount() == 0)
{
System.out.println("No feature selected.");
}else{
FeatureReader reader = fr.reader();
Feature f = null;
Vector vec_ident_feature = new Vector();
while (reader.hasNext())
{
f = (Feature) reader.next();
vec_ident_feature.add(f.getAttribute(0));
}
FeatureType ft_typ = f.getFeatureType() ;
if(state==1)
setColorOnDeterminedFeature(ft_typ,                   
 ft_typ.getAttributeType(0).getName(),
defaultStyle,vec_ident_feature,colorTrue,colorFalse);
else
setColorOnDeterminedFeature(ft_typ,
ft_typ.getAttributeType(0).getName(),
defaultStyle,vec_ident_feature,colorFalse,colorTrue);
state = 1 - state;
}

Finally the function that applicates a style on the
selected objects :
GeometryAttributeType l_geom_attrib_type =
ft_type_source.getDefaultGeometry();
StyleBuilder sb = new StyleBuilder();
Style mapStyle = null;
Style mapStyle_2 = null;
Long l_val;
long val_orig, val_calc;
try
{
StyleFactory styleFactory =
StyleFactory.createStyleFactory();
mapStyle = styleFactory.createStyle();
//the "true Style" depends of true Color
Mark markTrue =
sb.createMark(StyleBuilder.MARK_CIRCLE, colorTrue,
colorFalse, 1);
Graphic graphicsTrue = sb.createGraphic(null,
markTrue, null);
PointSymbolizer pointTrue=
sb.createPointSymbolizer(graphicsTrue);
//the "false Style" depends of False Color
Mark markFalse =
sb.createMark(StyleBuilder.MARK_CIRCLE, colorFalse,
colorTrue, 1);
Graphic graphicFalse = sb.createGraphic(null,
markFalse, null);
PointSymbolizer pointFalse=
sb.createPointSymbolizer(graphicFalse);

FilterFactory filterFactory =
FilterFactory.createFilterFactory();
CompareFilter filter = null;
FeatureTypeStyle fts = null;
Rule rule_other;
for(int i=0;i<vec_ident_feature.size();i++)
{
try
                    {
                        filter =
filterFactory.createCompareFilter(CompareFilter.COMPARE_EQUALS);
                       
filter.addLeftValue(filterFactory.createAttributeExpression(ft_type_source,ft_ident));
                        
                        l_val =
(Long)vec_ident_feature.get(i);
                        val_orig = l_val.longValue();
                        val_calc= (val_orig/2)*2;
                        
                       
filter.addRightValue(filterFactory.createLiteralExpression(val_orig));
                        //System.out.println("val_orig
:"+val_orig+" val_calc :"+val_calc + "ft_ident
:"+ft_ident+ (Long)vec_ident_feature.get(i));
                        if(val_orig==val_calc)
                        {    
                            
                            rule_other =
sb.createRule(pointTrue);
                        }else{
                            rule_other =
sb.createRule(pointFalse);                           
                        }    
                        rule_other.setFilter(filter);
                        if(fts==null)
                            fts =
sb.createFeatureTypeStyle(null, rule_other);
                        else
                            fts.addRule(rule_other);
                    }
                    catch (IllegalFilterException e) 
                    {
                        e.printStackTrace();
                    }
                }
                
                //valeur indeterminé, style par defaut
                /*{
                    Mark markFalse =
sb.createMark(StyleBuilder.MARK_CIRCLE, colorFalse,
colorTrue, 1);
                    Graphic graphicFalse =
sb.createGraphic(null, markFalse, null);
                    PointSymbolizer pointFalse=
sb.createPointSymbolizer(graphicFalse);

                    Rule ruleFalse =
sb.createRule(pointFalse);
                    //StyleVisitor
                    ruleFalse.setIsElseFilter(true);
                    ruleFalse.setName("Color False");
                    fts.addRule(ruleFalse);
                }*/
                
                Style sty =
styleFactory.createStyle();
                sty.addFeatureTypeStyle(fts); 
               
//m_styled_map_pane.getMapContext().getLayer(2).setStyle(sty);
                m_map_layer.setStyle(sty);
                //m_styled_map_pane.updateUI();
                //mapStyle.addFeatureTypeStyle(fts);
            }
            catch( Exception e ) 
            { 
              e.printStackTrace(); 
            }  


	

	
		
___________________________________________________________________________ 
Faites de Yahoo! votre page d'accueil sur le web pour retrouver directement vos services préférés : vérifiez vos nouveaux mails, lancez vos recherches et suivez l'actualité en temps réel. 
Rendez-vous sur http://fr.yahoo.com/set



More information about the postgis-users mailing list