java - getCurrentSession is creating new connection on each call -


i new in spring , hibernate, thing working fine checked on each call of getcurrentsession, new connection thread has been created in mysql goes in sleep state. know not practice database. why getcurrentsession creating new connection thread every time. code -

application-context.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">      <tx:annotation-driven />     <context:component-scan base-package="com.vc.teacher" />       <bean id="transactionmanager"         class="org.springframework.orm.hibernate4.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactory"></property>     </bean>      <bean id="sessionfactory"         class="org.springframework.orm.hibernate4.localsessionfactorybean"         lazy-init="false">         <property name="datasource" ref="datasource" />         <property name="annotatedclasses">             <list>                 <value>com.vc.teacher.entities.user</value>             </list>         </property>         <property name="hibernateproperties">             <props>                 <prop key="hibernate.dialect">org.hibernate.dialect.hsqldialect</prop>                 <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->                 <prop key="show_sql">true</prop>             </props>         </property>     </bean>        <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource">         <property name="driverclassname" value="com.mysql.jdbc.driver"></property>         <property name="url" value="jdbc:mysql://localhost:3306/teacher"></property>         <property name="username" value="root"></property>         <property name="password" value="root"></property>     </bean>      <bean id="userdao" class="com.vc.teacher.db.dao.userdao">         <property name="sessionfactory" ref="sessionfactory"></property>     </bean>  </beans> 

userdao class

package com.vc.teacher.db.dao;  import java.util.list;  import org.hibernate.query; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.springframework.beans.factory.annotation.autowired; import org.springframework.transaction.annotation.transactional;  import com.vc.teacher.entities.user;  public class userdao {       @autowired     sessionfactory sessionfactory;      public sessionfactory getsessionfactory() {         return sessionfactory;     }      public void setsessionfactory(sessionfactory sessionfactory) {         this.sessionfactory = sessionfactory;     }      @transactional     public user checkcreditionals(string email, string password){         user user = null;         session session  =  sessionfactory.getcurrentsession();         query query =   session.createquery("from user email = '"+email+"' , password = '"+password+"'");         list list   =   query.list();         if(list.size()>0)             user =  (user)list.get(0);          return user;     }      @transactional     public boolean registeruser(user user){         boolean result = false;         session session = sessionfactory.getcurrentsession();         try{             user.setusertypeid(2);             session.save(user);              result = true;         } catch (exception e){             result = false;             e.printstacktrace();         }          return result;     }  } 

entity class - user

package com.vc.teacher.entities;  import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table;  @entity @table(name="user") public class user {      @id     @generatedvalue     @column(name="user_id")     private int id;     @column(name="age")     private int age;     @column(name="user_type_id")     private int usertypeid;     @column(name="first_name")     private string firstname;     @column(name="last_name")     private string lastname;     @column(name="profession")     private string profession;     @column(name="phone")     private string phone;     @column(name="email")     private string email;     @column(name="password")     private string password;     @column(name="address")     private string address;     @column(name="status")     private string status;       public int getid() {         return id;     }     public void setid(int id) {         this.id = id;     }     public int getage() {         return age;     }     public void setage(int age) {         this.age = age;     }      public int getusertypeid() {         return usertypeid;     }     public void setusertypeid(int usertypeid) {         this.usertypeid = usertypeid;     }     public string getfirstname() {         return firstname;     }     public void setfirstname(string firstname) {         this.firstname = firstname;     }     public string getlastname() {         return lastname;     }     public void setlastname(string lastname) {         this.lastname = lastname;     }     public string getprofession() {         return profession;     }     public void setprofession(string profession) {         this.profession = profession;     }     public string getphone() {         return phone;     }     public void setphone(string phone) {         this.phone = phone;     }     public string getemail() {         return email;     }     public void setemail(string email) {         this.email = email;     }     public string getpassword() {         return password;     }     public void setpassword(string password) {         this.password = password;     }     public string getaddress() {         return address;     }     public void setaddress(string address) {         this.address = address;     }     public string getstatus() {         return status;     }     public void setstatus(string status) {         this.status = status;     }  } 

controller class - accountcontroller

package com.vc.teacher.controller;  import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.servlet.modelandview; import org.springframework.web.servlet.mvc.support.redirectattributes;  import com.vc.teacher.db.dao.userdao; import com.vc.teacher.entities.user;  @controller public class accountcontroller {      @requestmapping("/login")     public string loginuser(@requestparam("email") string email,             @requestparam("password") string password, model model) {         applicationcontext context = new classpathxmlapplicationcontext(                 "applicationcontext.xml");         user user = ((userdao) context.getbean("userdao")).checkcreditionals(                 email, password);         if (user != null) {             model.addattribute("user", user);             return "jsp/profile";         } else {             model.addattribute("error", "wrong creditionals");             return "jsp/signin";         }      }      @requestmapping("/signup")     public string initilize(model model) {         model.addattribute(new user());         return "jsp/signup";     }      @requestmapping(method = requestmethod.post, value = "/register")     public string signupuser(user user, redirectattributes attributes) {         boolean result = false;          applicationcontext context = new classpathxmlapplicationcontext(                 "applicationcontext.xml");         user.setstatus("deactive");         result = ((userdao) context.getbean("userdao")).registeruser(user);          if (result == true) {             attributes.addflashattribute("message", "you ready go !");             return "redirect:/signup";         } else {             attributes.addflashattribute("message", "something went wrong");             return "redirect:/signup";         }      } } 

mysql connection status snapshot -

enter image description here

load context listener using deployment descriptor following entry.

<listener> <listen-class>org.springframework.web.context.contextloaderlistener </listener-class> </listener>

you have defined component scan package, can use autowiring here, add @component annotation userdao , remove following code service layer

 applicationcontext context = new classpathxmlapplicationcontext(             "applicationcontext.xml");  user user = ((userdao) context.getbean("userdao")) 

define dao class @component annotation.

package com.vc.teacher.db.dao;  import java.util.list;  import org.hibernate.query; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.springframework.beans.factory.annotation.autowired; import org.springframework.transaction.annotation.transactional;  import com.vc.teacher.entities.user; @component public class userdao {   @autowired sessionfactory sessionfactory;  public sessionfactory getsessionfactory() {     return sessionfactory; }  public void setsessionfactory(sessionfactory sessionfactory) {     this.sessionfactory = sessionfactory; }  @transactional public user checkcreditionals(string email, string password){     user user = null;     session session  =  sessionfactory.getcurrentsession();     query query =   session.createquery("from user email = '"+email+"' , password = '"+password+"'");     list list   =   query.list();     if(list.size()>0)         user =  (user)list.get(0);      return user; }  @transactional public boolean registeruser(user user){     boolean result = false;     session session = sessionfactory.getcurrentsession();     try{         user.setusertypeid(2);         session.save(user);          result = true;     } catch (exception e){         result = false;         e.printstacktrace();     }      return result; }  }` 

now initiate object this-

@autowired userdao userdao; 

and call method-

userdao.checkcreditionals(email, password); 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -