Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3986224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:59:09+00:00 2026-05-20T05:59:09+00:00

I have a servlet where I get a Connection object which is then handed

  • 0

I have a servlet where I get a Connection object which is then handed to two worker threads for various activities. I now need to add a transaction on one thread.

If I start a transaction like this:
connection.setAutoCommit(false);

would that impact both threads? I think it would.

Do I have to get a separate connection of each thread?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:59:10+00:00Added an answer on May 20, 2026 at 5:59 am

    I think what you are doing is very bad practice. You can’t share a JDBC connection among threads.

    If you are running under an application server (like TOMCAT/JBoss/WebSphere/WebLogic) use a proper DataSource to get your connections as you need them.

    Look at your Application Server documentation to get information on how to do that.

    You will have something like this in your servlet:

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        Connection c = null;
        try {
            c = ...; /* preferred way of getting a connection in your AppServer
            // do what you need with your JDBC connection
        } catch (Exception e) {
            // handle errors
        } finally {
            c.close(); /* you will need another TRY/CATCH here */
        }
    }
    

    Similarly, your worker threads will have something like:

    public void run()
    {
        Connection c = null;
        try {
            c = ...; /* preferred way of getting a connection in your AppServer
            // do what you need with your JDBC connection
        } catch (Exception e) {
            // handle errors
        } finally {
            c.close(); /* you will need another TRY/CATCH here */
        }
    }
    

    Eventually, you could set auto commit to whatever you need on separate connection objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main Servlet that processes post/get requests. I am using connection pooling
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I have a servlet which takes us to an existing jsp, say home.jsp. This
I have a Servlet that sends back a JSON Object and I would like
I occasionally get an IOException from a Servlet I have that writes a byte
I have this managed bean which takes two values from database's table import java.io.Serializable;
I have an applet trying to send java serialized object to a servlet (hosted
I have a JSP file Signup.jsp which submits to UserDetails servlet which inserts the
I have written code which should call MSSQL 2005 procedure from my servlet. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.