1

Thanks in advance for giving attention. Here i am having good idea about mvc pattern of jsp servlet and jdbc as beginner. My scenario is

  • mysql table employee so pojo class Employee
  • EmployeeDao for dao operations
  • Service class for authentication of login & other services
  • Servlet to fetch data from jsp page and calling service

Now i am not sure about best way to create connection where i found some ideas below,

  1. creating jdbc connection in EmployeeDao constructor by loading driver. Service for creating dao object and perform operations and then clearing connection using same object's method destroy to close connection.
  2. loading driver and creating connection at init() method of servlet? if so, then do i need to pass connection object created in init to service so it will pass dao further? And destroy() of servlet will be used to close connection.
  3. Using ServletListener or ServletContextListener ( not having brief idea about these ( needs some documents for study)

Till now I was using Dao class for connection and loading driver but i need to optimise it and make my application efficient. Correct me if I am misleading somewhere. thanking you

  • Ideally, use a framework that handles all of this for you--about 90% of what you described is available out-of-the-box with Spring Boot, Spring MVC, and Spring Security. – chrylis -cautiouslyoptimistic- Mar 20 '17 at 07:06
  • None of these. Get a connection when you need it, and commit it as soon as your transaction is done. You can't (and don't want to) share a connection between threads serving concurrent requests. – JB Nizet Mar 20 '17 at 07:07
  • create a separate package for connections. Establish to functions to get and close connection. Whenever you need a connection call the getConnection method once you done with your thing you can call closeConnection method – Anoop LL Mar 20 '17 at 07:11
  • @JBNizet yes that is first case. EmployeeDao is called by service when service related to database operation is needed. But while no. of services called related to database transaction by user, Single application open and close database connection several time. So i wanted to do it once to reduce complexity. – Rajesh Navagare Mar 20 '17 at 07:17
  • @AnoopLL yes, same thing is done by EmployeeDao. Its constructor sets connection and after transactions and select queries, same object is forced to call destoyConnection() method which shut down connection. – Rajesh Navagare Mar 20 '17 at 07:20
  • The DAO constructor doesn't need a connection. A DAO is typically instantiated once and reused. And a service method usually uses more than one DAO. I concur with chrylis: use Spring declarative transactions and let it handle this for you. – JB Nizet Mar 20 '17 at 07:20

3 Answers3

1

Use Spring MVC along with Spring JDBC (for handling DAO) that is suffice for above requirement.

Porkko M
  • 307
  • 2
  • 10
1

So, the best solution is next:

  1. Create ServletContextListener
  2. In contextInitialized method get dataSource connection pool.
  3. In contextInitialized method create all services, that you need, and inject dataSouce into services using constructors(Preferable than setters).
  4. Put all services into ServletContext.
  5. In init method of each servlet get services, that you need for specified servlet.
  6. Be happy, and use them :)
Valerii
  • 106
  • 9
0

Definitely Spring MVC will be a good option. But if you are bound to use in Servlet MVC, then you can go for the below links which might be helpful for you.

If you only want to create JDBC connection without Datasource, go here

If you want to create through Datasource, go here

Community
  • 1
  • 1
Arindam
  • 555
  • 1
  • 8
  • 24