Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-threaded server database access

I'm writing a multi-threaded server in Java that will access a database and return information about the database that is requested by clients.

My main server class is written as a static class that basically spawns a thread when a client connects to it.

My question is where to put all of the database access methods? Should they be located in the static main server class or should they be put in the server thread code?

All answers/explanations are appreciated!

like image 912
eric121492 Avatar asked Mar 16 '26 07:03

eric121492


1 Answers

It should be kept in thread part. Because if you use access methods in static server class, then the session object created for database interaction will become thread unsafe. There will be a possibility that your database is left with inconsistencies.

It is advisable that the data base connection part is kept in thread code. It will be good idea to use thread pool. You can tune your performance for database intensive applications.Following are two important docs from oracle regarding this issue.

  1. http://docs.oracle.com/cd/E19159-01/819-3681/abehq/index.html
  2. http://docs.oracle.com/cd/E19159-01/819-3681/abehs/index.html
like image 175
nirmesh khandelwal Avatar answered Mar 17 '26 20:03

nirmesh khandelwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!