2
I have a problem regarding open connections in the database, where error is occurring if the application tries to create more connections than the limit. Assuming the application creates and closes the connections for each interaction in the database (Insert/update/select), how to make the application "wait" for a new connection? I am using Hibernate + java.
EntityManagerFactory factory = Persistence.createEntityManagerFactory("model");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(produto);
manager.getTransaction().commit();
manager.close();
factory.close();
Edit
I took a look over SessionFactory
, configured the app to use Connection Pool hibernate.cfg.xml
, but the error still occurs.
Hibernateutils.java
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Here the Save Method
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(produto);
session.getTransaction().commit();
Hibernate.cfg.xml
<!-- Pool de conexão -->
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">4</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- entidade mapeada -->
<mapping class="br.com.sales.model.ProdutosVO" />
This might help: http://www.devmedia.com.br/connection-pool/5869
– Ricardo
@Ricardo I have configured, but no effects have arisen. I have a pending question about this: http://answall.com/questions/130234/c3p0-connection-pool
– Matheus
Dude, I think the solution is the child of a Singleton Bank Class, so the Bank only has one instance in the entire program and can’t afford to create more than one connection to the bank. Here you find more on this
– João Gabriel Machado