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 it, but postgres limits me to the connection number.
Nowfollowtheerror:
Set12,20168:53:39AMorg.hibernate.tool.hbm2ddl.SchemaUpdateexecuteERROR:HHH000299:Couldnotcompleteschemaupdateorg.postgresql.util.PSQLException:FATAL:sorry,toomanyclientsalreadyatorg.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:398)atorg.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:173)atorg.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)atorg.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)atorg.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)atorg.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)atorg.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)atorg.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)atorg.postgresql.Driver.makeConnection(Driver.java:410)atorg.postgresql.Driver.connect(Driver.java:280)atorg.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)atorg.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)atorg.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:219)atorg.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:203)atorg.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:509)atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1799)atorg.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)atorg.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)atorg.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)atorg.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)atjavax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)atjavax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)atfactory.ProdutorEntityManager.<init>(ProdutorEntityManager.java:10)atcom.arquivo.Ler.main(Ler.java:25)Set12,20168:53:39AMorg.hibernate.ejb.internal.EntityManagerFactoryRegistryaddEntityManagerFactoryWARN:HHH000436:Entitymanagerfactoryname(bd)isalreadyregistered.Ifentitymanagerwillbeclusteredorpassivated,specifyauniquevalueforproperty'hibernate.ejb.entitymanager_factory_name'Set12,20168:53:39AMorg.hibernate.engine.jdbc.spi.SqlExceptionHelperlogExceptionsWARN:SQLError:0,SQLState:53300Set12,20168:53:39AMorg.hibernate.engine.jdbc.spi.SqlExceptionHelperlogExceptionsERROR:FATAL:sorry,toomanyclientsalreadyorg.hibernate.exception.GenericJDBCException:Couldnotopenconnection
Sorry,followtheCode:
publicstaticvoidmain(String[]args)throwsFileNotFoundException{List<Dados>arquivo=newArrayList<Dados>();Dadosd=newDados();Scannerscanner=newScanner(newFileReader("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());
}
}