简体   繁体   中英

what is the best way to open transaction and have connections with DB in Spring mvc

i have deployed my spring mvc appliction in server(in production) later some months my application crashing tomcat server i asked application hosting company why is it happening .

they said that no. of war file in server are more and your connections to db are more need to close them after transaction.

but my point is i am using spring mvc framework , which takes care of connection opening and closing .but the hosting team tells me that organize your mysql

is my approach is correct or not please have a look on my code

controller :

 @GetMapping("/getStudentDetails")
 public ResponseEntity<Map<String, Object>> getStudentDetails()
{
    List<Students> stus= examService.getStudentDetails();
}

Service Implementation

@Override
@Transactional  //here starts only one transaction
public List<Students> getStudentDetails( ) {

    return examDao.getStudentDetails();
} 

DAO Implementation

 @Override
        public List<Students> getStudentDetails() {
    // here i will have no of connections to get student details as follows

currentSession = sessionFactory.getCurrentSession();
//get student details
Query query=currentSession .createQuery(" from  StudentDetais");
query.list();

//get student parent details
Query query=currentSession .createQuery(" from  StudentParentDetais");
query.list();
    }

//get student personal details
Query query=currentSession .createQuery(" from  StudentPersonalDetais");
query.list();
    }
...........
............. so on...

in the above code with one transaction i took no of connections to db , like that for each and every student i will loop same queries without going back to controller.

is this approach spoiling my application ?

shall i @Transactional for each and every student so that for ever student connection will be opened and closed.

send one student id from controller and have transaction and close and the send another student id from controller and do transaction....so on.

please solve my problem that can i have no of connections in one transaction? thank you.

JPA query may return list of proxies. JPA may keep using connection until getters invoked on proxies. Solution might be to use DTOs and feel them after every query with data returned by JPA layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM