title: Is it good practice to use ThreadLocal for JDBC Connections?
body: <p>I am not sure if I get this abous ThreadLocals. Sometimes you can read that a common practice is to make JDBC connections as ThreadLocals so each thread gets its own copy of the connection. Let´s assume the socket below is a JDBC connection. Then I do:</p>
  
  <pre><code>public ThreadLocalSocket() throws IOException {
  
      Runnable runnable = new Runnable() {
  
          ThreadLocal&lt;Socket&gt; threadLocal = new ThreadLocal&lt;&gt;();
          @Override
          public void run() {
              try {
                  threadLocal.set(new Socket("www.google.com", 80));
              } catch (IOException e) {e.printStackTrace();}
  
              Thread thread = Thread.currentThread();
              for (int j = 0; j &lt; 10 ; j++) {
  
                  Socket sock = threadLocal.get();
                  if (thread.getName().equals("t1")) {
                      if (!sock.isClosed()) {
                          try {
                              sock.close();
                          } catch (IOException e) {e.printStackTrace();}
                      }
                  }
                  System.out.println("Thread: " + thread.getName()+  ", is closed? " + sock.isClosed() + ", sock hashCode = " + sock.hashCode());
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {e.printStackTrace();}
              }
          }
      };
  
      Thread t1 = new Thread(runnable);
      t1.setName("t1");
      Thread t2 = new Thread(runnable);
      t2.setName("t2");
  
      t1.start();
      t2.start();
  }
  </code></pre>
  
  <p>Why cannot I simply do this without ThreadLocals? The behaviour is exactly the same in the following code snippet as the code example above: </p>
  
  <pre><code>public ThreadLocalSocket() throws IOException {
  
      Runnable runnable = new Runnable() {
          @Override
          public void run() {
              Socket socket = null;
              try {
                  socket = new Socket("www.google.com", 80);
              } catch (IOException e) {e.printStackTrace();}
  
              Thread thread = Thread.currentThread();
              for (int j = 0; j &lt; 10 ; j++) {
                  if (thread.getName().equals("t1")) {
                      if (!socket.isClosed()) {
                          try {
                              socket.close();
                          } catch (IOException e) {e.printStackTrace();}
                      }
                  }
                  System.out.println("Thread: " + thread.getName()+  ", is closed? " + socket.isClosed() + ", socket hashCode = " + socket.hashCode());
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {e.printStackTrace();}
              }
          }
      };
  
      Thread t1 = new Thread(runnable);
      t1.setName("t1");
      Thread t2 = new Thread(runnable);
      t2.setName("t2");
  
      t1.start();
      t2.start();
  }
  </code></pre>