Persisting in Database from a file

Asked

Viewed 180 times

2

What would be the most efficient way to read a file and write to the Postgres database? I’m posting a code where I can do, but postgres limits me on the connection number.

inserir a descrição da imagem aqui

Now follow the error:

Set 12, 2016 8:53:39 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000299: Could not complete schema update
org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:398)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:173)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:410)
    at org.postgresql.Driver.connect(Driver.java:280)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:219)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:203)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:509)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1799)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at factory.ProdutorEntityManager.<init>(ProdutorEntityManager.java:10)
    at com.arquivo.Ler.main(Ler.java:25)

Set 12, 2016 8:53:39 AM org.hibernate.ejb.internal.EntityManagerFactoryRegistry addEntityManagerFactory
WARN: HHH000436: Entity manager factory name (bd) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
Set 12, 2016 8:53:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 53300
Set 12, 2016 8:53:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: FATAL: sorry, too many clients already
org.hibernate.exception.GenericJDBCException: Could not open connection

Sorry follow the Code:

public Static void main(String[] args) throws Filenotfoundexception {

    List<Dados> arquivo = new ArrayList<Dados>();
    Dados d = new Dados();
    Scanner scanner = new Scanner(new FileReader("C:\\ler/arquivo1.txt")).useDelimiter("\\||\\n");


    try {

        while (scanner.hasNext()) {

            ProdutorEntityManager mf = new ProdutorEntityManager();

               d.setId(null);   
               d.setNome(scanner.next());
               d.setCidade(scanner.next());
               d.setEstado(scanner.next());
               arquivo.add(d);

               mf.manager.getTransaction().begin();
               mf.manager.persist(d);
               mf.manager.getTransaction().commit();
               mf.manager.close();

        }

    } catch (Exception e) {

            System.out.println(e.getMessage());
    }

}
  • 1

    Post the code in the question too, without it can not analyze the problem.

  • As @Leonardo said, you don’t need to instantiate a new Entitymanager every while loop.

  • Posting the code means placing its text and not image.

  • And how would I do because I put the instance outside the while does not rotate (see comment below) and I imagine that I can not keep some 10,000 obj in memory and then persist them.

1 answer

1


I believe if you move the instruction ProdutoEntityManager mf = new ProdutoEntityManager(); out of the while, it will work, as there is no need to create several EntityManager to persist more than one object, so will no longer occur this your exception.

  • I have already done this happens that the method "mf.manager.close();" closes the connection in the first past of while and does not allow persistence. I also tried to put the up method out of the while but get the following exception.

  • I’ve done this but at first I’ve run while the "mf.manager.close();" method closes the connection, I’ve tried to put the same method out of while however this is the exception I get: INFO: HHH000232: Schema update complete Hibernate: select nextval ('hibernate_sequence') Hibernate: Insert into Dados (city, state, name, id) values (?, ?, ??) Error while committing the transaction

  • Also remove the instruction mf.manager.close(); out of the while.

  • Another question, this is a web application?

  • It’s not web. I already did this when I put out the while I have this exception: Error while committing the transaction

  • Give me a better description of stackerror.

  • Show me how the mapping of your entities is probably something in the mapping.

  • Staff I managed to solve was what you talked about but had to instate the Object Class within the while.

  • Now that I’ve seen that dados on the court side, yes it makes sense.

  • vlw I am new in development and on the site I need to close this topic?

  • No, it’s just interesting to do what you did which is to mark the question as answered and mark the answer as useful.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.