IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

[jdbc] postgresql plantage


Sujet :

JDBC Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 54
    Points : 31
    Points
    31
    Par défaut [jdbc] postgresql plantage
    bonjour,

    j'ai une application java qui alimente une table postgresql

    le vecteur qui est en parametre il a une taille 50000 voila le code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    public int addTownsTest(Vector arg) {
     
            Connection conn = null;
            PreparedStatement stmt = null;
            Towns supdt = null;
            int rs = 0;
            for (int i = 0; i < arg.size(); i++) {
     
                supdt = (Towns) arg.elementAt(i);
                String query = "INSERT INTO Towns (idtowns,postalcode,town) VALUES(?,?,?)";
                try {
                    conn = init();
                    stmt = conn.prepareStatement(query);
                    stmt.setInt(1, supdt.getIdtown());
                    stmt.setString(2, supdt.getCodepostal());
                    stmt.setString(3, supdt.getTown());
                    rs = stmt.executeUpdate();
                    stmt.close();
     
                } catch (SQLException e) {
                    System.out.println("Requete ajout towns incorrecte");
                    e.printStackTrace();
                } catch (ClassNotFoundException cnf) {
                    System.out
                            .println("La classe du driver jdbc n'a pu etre chargee");
                    cnf.printStackTrace();
                }
     
                finally{
                     try {
                        conn.close();
     
                    } catch (SQLException e) {
                    }
                }
            }
            return rs;
        }
    et au bout de 3900 ligne inserrées j'ai les messages d'erreurs suivants:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     
    org.postgresql.util.PSQLException: La tentative de connexion a échoué.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
        at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
        at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
        at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
        at org.postgresql.Driver.makeConnection(Driver.java:382)
        at org.postgresql.Driver.connect(Driver.java:260)
        at java.sql.DriverManager.getConnection(DriverManager.java:549)
        at java.sql.DriverManager.getConnection(DriverManager.java:181)
        at pack.DriverJDBC.initDriverpgsql(DriverJDBC.java:31)
        at com.vgo.controller.ResellersController.init(ResellersController.java:59)
        at com.vgo.controller.ResellersController.addTownsTest(ResellersController.java:1230)
        at com.vgo.controller.ResellersController.addTowns(ResellersController.java:1209)
        at pack.Mains.main(Mains.java:73)
    Caused by: java.net.BindException: Address already in use: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)Requete ajout towns incorrecte
     
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:516)
        at java.net.Socket.connect(Socket.java:466)
        at java.net.Socket.<init>(Socket.java:366)
        at java.net.Socket.<init>(Socket.java:179)
        at org.postgresql.core.PGStream.<init>(PGStream.java:60)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
        ... 13 more
    est ce qu'il y a quelqu'un qui a une petite idée

    merci

  2. #2
    Membre éclairé Avatar de g_rare
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    608
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 608
    Points : 683
    Points
    683
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    public int addTownsTest(Vector arg) {
            int rs = 0;       
            try {
    // utiliser la même connexion (ton erreur venait du fait que la base
    // ne pouvait plus te procurer de nouvelles ressources au bout d'un moment)         
            Connection conn = conn = init();         
    String query = "INSERT INTO Towns (idtowns,postalcode,town) VALUES(?,?,?)";
            // précompiler une seule fois ta requête dans le moteur de base
            PreparedStatement stmt = conn.prepareStatement(query); 
            for (int i = 0; i < arg.size(); i++) {
                Towns supdt = (Towns) arg.elementAt(i);
                try {                                
                    stmt.setInt(1, supdt.getIdtown());
                    stmt.setString(2, supdt.getCodepostal());
                    stmt.setString(3, supdt.getTown());
                    rs += stmt.executeUpdate(); // sinon c'est le nombre de ligne affectées par le dernier update qui est renvoyé 
                } catch (SQLException e) {
                    System.out.println("Requete ajout towns incorrecte");
                    e.printStackTrace();
                }      
                finally{                
                     try {
                         if (stmt != null) {
                           stmt.close();
                         }
                         if (conn != null) {
                          conn.close();
                        }
                    } catch (SQLException e) { // ne rien faire
                }
            }
            } catch (ClassNotFoundException cnf) {
                    System.out
                            .println("La classe du driver jdbc n'a pu etre chargee");
                    cnf.printStackTrace();
                }        
            return rs;
        }

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 54
    Points : 31
    Points
    31
    Par défaut
    ça marche toujours pas et j'ai ça comme erreur


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    org.postgresql.util.PSQLException: Ce statement a été fermé.
    	at org.postgresql.jdbc2.AbstractJdbc2Statement.checkClosed(AbstractJdbc2Statement.java:2409)
    	at org.postgresql.jdbc2.AbstractJdbc2Statement.setInt(AbstractJdbc2Statement.java:1132)
    	at com.vgo.controller.ResellersController.addTowns(ResellersController.java:1137)
    	at pack.Mains.main(Mains.java:75)

  4. #4
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    ben je dirais qu'il faudrait plutot fermer le statement et la connexion en dehors du for...

  5. #5
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Salut,


    Tu as cette erreur car tu fermes deux fois le statement...
    De plus il vaut mieux utiliser le bloc try/catch un niveau au dessus des try/finally pour bien libéré proprement les resources tout en gérant correctement les exceptions, par exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
        public int addTownsTest(Vector arg) {
            int rs = 0;
            try {
                Connection conn = init();
                try {
                    
                    String query = "INSERT INTO Towns (idtowns,postalcode,town) VALUES(?,?,?)";
                    PreparedStatement stmt = conn.prepareStatement(query);
                    try {
                        for (int i = 0; i < arg.size(); i++) {
                            Towns supdt = (Towns) arg.elementAt(i);
                            stmt.setInt(1, supdt.getIdtown());
                            stmt.setString(2, supdt.getCodepostal());
                            rs += stmt.executeUpdate();
                        }
                    } finally {
                        stmt.close();
                    }
                        
                } finally {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }
    Ou mieux, tu peux faire remonter l'exception :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
        public int addTownsTest(Vector arg) throws SQLException {
            int rs = 0;
            Connection conn = init();
            try {
                
                String query = "INSERT INTO Towns (idtowns,postalcode,town) VALUES(?,?,?)";
                PreparedStatement stmt = conn.prepareStatement(query);
                try {
                    for (int i = 0; i < arg.size(); i++) {
                        Towns supdt = (Towns) arg.elementAt(i);
                        stmt.setInt(1, supdt.getIdtown());
                        stmt.setString(2, supdt.getCodepostal());
                        rs += stmt.executeUpdate();
                    }
                } finally {
                    stmt.close();
                }
                    
            } finally {
                conn.close();
            }      
            return rs;
        }
    a++

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 54
    Points : 31
    Points
    31
    Par défaut
    merci beaucoup
    ça marche avec le code de adiGuba


+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Pb jdbc postgresql
    Par Fahmi06 dans le forum JDBC
    Réponses: 5
    Dernier message: 27/06/2007, 21h48
  2. JDBC, postgreSQL et Eclipse
    Par volontier dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 26/06/2006, 10h06
  3. [JDBC][POSTGRESQL] Comment récupérer un OID
    Par petitpasdelune dans le forum JDBC
    Réponses: 1
    Dernier message: 07/06/2006, 15h15
  4. [JDBC] [PostgreSQL] Insert d'un serial
    Par e1lauren dans le forum JDBC
    Réponses: 3
    Dernier message: 16/05/2006, 15h57
  5. JBuilder 8 et JDBC/PostgreSQL
    Par nicox dans le forum JBuilder
    Réponses: 2
    Dernier message: 14/05/2003, 15h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo