Hello everyone, jpa has a long connection to the database. The database is located on a remote server. function that calls entityManager

public List<Posts> connect() throws UnsupportedEncodingException { EntityManager em=EntityManagerUtil.getEntityManagerFactory().createEntityManager(); Query q=em.createQuery("Select p from Posts p"); result=q.getResultList(); return result; } 

persistence.xml

org.hibernate.jpa.HibernatePersistenceProvider

  <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/temp"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="user"/> <property name="hibernate.connection.password" value="user03022016"/> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hbm2ddl.auto" value="update"/> </properties> </persistence-unit> 

  • "long" is how many seconds? - Nofate
  • @Nofate, about 4-5 seconds - andy

1 answer 1

Use the connection pull dataSource. This will avoid creating connections in the process of working with the application and limit the number of simultaneously open connections to the database.

There are examples of how to configure this in Spring https://stackoverflow.com/questions/5117191/spring-jdbc-connection-pool-best-practices

In your case, you can use the implementation of the pool C3P0 (at the end zero and not "O")

  • Thanks, I'll try - andy