Guest User

Untitled

a guest
Aug 20th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. ERROR: Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/hb_student_records?useSSL=false&serverTimezone=UTC
  2. Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
  3.  
  4. package com.rsharma.hibernate.demo;
  5.  
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8. import org.hibernate.cfg.Configuration;
  9.  
  10. import com.rsharma.hibernate.demo.entity.Student;
  11.  
  12. public class CreateStudentDemo {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. // create session factory
  17. SessionFactory factory = new Configuration()
  18. .configure("hibernate.cfg.xml")
  19. .addAnnotatedClass(Student.class)
  20. .buildSessionFactory();
  21.  
  22. // create session
  23. Session session = factory.getCurrentSession();
  24.  
  25. try {
  26. // create a student object
  27. System.out.println("Creating new student object...");
  28. Student tempStudent = new Student("Rishav", "Sharma", "paul@luv2code.com");
  29.  
  30. // start a transaction
  31. session.beginTransaction();
  32.  
  33. // save the student object
  34. System.out.println("Saving the student...");
  35. session.save(tempStudent);
  36.  
  37. // commit transaction
  38. session.getTransaction().commit();
  39.  
  40. System.out.println("Done!");
  41. }
  42. finally {
  43. factory.close();
  44. }
  45. }
  46.  
  47. }
  48.  
  49. package com.rsharma.hibernate.demo.entity;
  50.  
  51. import javax.persistence.Column;
  52. import javax.persistence.Entity;
  53. import javax.persistence.GeneratedValue;
  54. import javax.persistence.GenerationType;
  55. import javax.persistence.Id;
  56. import javax.persistence.Table;
  57.  
  58. @Entity
  59. @Table(name="student")
  60. public class Student {
  61.  
  62. @Id
  63. @GeneratedValue(strategy=GenerationType.AUTO)
  64. @Column(name="id")
  65. private int id;
  66.  
  67. @Column(name="first_name")
  68. private String firstName;
  69.  
  70. @Column(name="last_name")
  71. private String lastName;
  72.  
  73. @Column(name="email")
  74. private String email;
  75.  
  76.  
  77.  
  78. public Student(){
  79.  
  80. }
  81.  
  82.  
  83. public Student(String firstName, String lastName, String email) {
  84. this.firstName = firstName;
  85. this.lastName = lastName;
  86. this.email = email;
  87. }
  88.  
  89.  
  90. public int getId() {
  91. return id;
  92. }
  93.  
  94.  
  95. public void setId(int id) {
  96. this.id = id;
  97. }
  98.  
  99.  
  100. public String getFirstName() {
  101. return firstName;
  102. }
  103.  
  104.  
  105. public void setFirstName(String firstName) {
  106. this.firstName = firstName;
  107. }
  108.  
  109.  
  110. public String getLastName() {
  111. return lastName;
  112. }
  113.  
  114.  
  115. public void setLastName(String lastName) {
  116. this.lastName = lastName;
  117. }
  118.  
  119.  
  120. public String getEmail() {
  121. return email;
  122. }
  123.  
  124.  
  125. public void setEmail(String email) {
  126. this.email = email;
  127. }
  128.  
  129.  
  130. @Override
  131. public String toString() {
  132. return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
  133. }
  134.  
  135. }
  136.  
  137. <!DOCTYPE hibernate-configuration PUBLIC
  138. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  139. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  140.  
  141. <hibernate-configuration>
  142.  
  143. <session-factory>
  144.  
  145. <!-- JDBC Database connection settings -->
  146. <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
  147. <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_records?useSSL=false&serverTimezone=UTC</property>
  148. <property name="connection.username">hbstudent</property>
  149. <property name="connection.password">hbstudent</property>
  150.  
  151. <!-- JDBC connection pool settings ... using built-in test pool -->
  152. <property name="connection.pool_size">1</property>
  153.  
  154. <!-- Select our SQL dialect -->
  155. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  156.  
  157. <!-- Echo the SQL to stdout -->
  158. <property name="show_sql">true</property>
  159.  
  160. <!-- Set the current session context -->
  161. <property name="current_session_context_class">thread</property>
  162.  
  163. </session-factory>
  164.  
  165. </hibernate-configuration>
Add Comment
Please, Sign In to add comment