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.
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());
}
}
Post the code in the question too, without it can not analyze the problem.
– user28595
As @Leonardo said, you don’t need to instantiate a new Entitymanager every while loop.
– DiegoAugusto
Posting the code means placing its text and not image.
– Maniero
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.
– Lourival Santos