Announcement Announcement Module
No announcement yet.
new thread in service layer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • new thread in service layer


    maybe it's a common problem but couldn't find any similiar thread here in forum

    the problem is:
    I enter from WebController to Service layer w/ declarative transactional management.
    In service layer I do my job(like CRUD operation on a table) and before return to WebController
    I create a new Thread which does another job (like sending e-mail, sms...)

    so what happens? after a while the applications freezes and doesn't responde to any request.

    first of all I don't like what I do
    because, I don't know what happens to connection...
    I enter w/ 1 connection,
    I exit. What happens to that connection? commit/rollback?

    What happens to the new thread? How it gets the new connection?

    I couldn't find any solution for 2 weeks,
    so any help would be appreciated

  • #2
    Creating threads inside web application is a bad practice and the outcome cannot be expected to be good. For a job like the one you need to accomplish, you should set up a JMS queue in your application server and, instead of creating new thread, you should send a message from your service and then go back to controller. A scheduled job can then periodically poll the JMS queue and, in case a message is found, elaborate it (send it by sms, by mail etc.). This is the standard way to do such things in a web application, and btw Spring offers great JMS integration facilities and has its own job scheduling system which is practical and easy to use.

    Spring has its own mechanism to ensure thread-safety (one big part of it is using default singleton beans) and to ensure proper handling of connections lifecycle. If you do ugly things like messing up with threads or db connections yourself, you are working against the framework and you can expect nothing good to come from it.