Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a database connection inside a thread

 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread in my application that instantiates a class called DBConnect which has a getConnection method :



This thread uses the DBConnect object to get a database connection. This thread then has to spawn other threads which all also need to make database connections. Right now, to spawn the other threads, I am extending the class java.util.TimerTask. In the run method of the TimerTask, I create a new object of DBConnect. I think that using this class is probably wrong, that I should just start a new java Thread, because I am getting the following error inside my TimerTask code :
SEVERE: Database.constructor: Error connecting or reading table:Server connection failure during transaction. Due to underlying exception: 'java.net.BindException: Address already in use: connect'.



** BEGIN NESTED EXCEPTION **



java.net.BindException

MESSAGE: Address already in use: connect



STACKTRACE:



java.net.BindException: Address already in use: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(Unknown Source)

at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:124)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)

at com.mysql.jdbc.Connection.createNewIO(Connection.java:1899)

at com.mysql.jdbc.Connection.<init>(Connection.java:450)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at com.radianse.alerts.utility.DBConnect.getConnection(Unknown Source)

at com.radianse.alerts.repository.AlertBucketDAO.getAlertBucketItems(Unknown Source)

at com.radianse.alerts.businessobjects.AlertBucketImpl.getBucketItems(Unknown Source)

at com.radianse.alerts.jms.AlertEvaluator.run(Unknown Source)

** END NESTED EXCEPTION **

Attempted reconnect 3 times. Giving up.

My question is,

  • should I stop using the TimerTask
  • should I use the DBConnect object I already have in the thread that spawns the TimerTask in the TimerTask run method instead of making a new object of it
  • should I use some other method to achieve what I am trying to do ?

  • Thank you for any input,
    Kim
     
    Henry Wong
    author
    Sheriff
    Posts: 22542
    109
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My question is,

  • should I stop using the TimerTask
  • should I use the DBConnect object I already have in the thread that spawns the TimerTask in the TimerTask run method instead of making a new object of it
  • should I use some other method to achieve what I am trying to do ?

  • Thank you for any input,

    This is not really a threads issue. It looks like a networking issue. You are trying to open a connection to a port that is already in use, probably by another DBconnect object.

    So... given the three choices in this multiple choice question, I guess I would go with the second one.

    Henry
     
    Kim Kantola
    Ranch Hand
    Posts: 276
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, thanks Henry for replying. This is too funny because I was thinking I need to understand this better so I went out on my lunch break and bought your book "Java Threads"!

    It has already helped me on one thing and I was wondering if I could confirm this with you ? I was reading more about the Timer class and realized that I had misunderstood it. My one thread that has a constant while(true) loop will periodically need to start the new TimerTasks. I thought I needed to create a new Timer() object for each task, but if I am understanding correctly I can just make one Timer class, and then add the tasks to it as needed, thus cutting down on the number of threads greatly. Is that correct? The Timer object itself starts a thread, and then all the tasks will get handled by that one thread?

    Thanks again very much for your reply.
    Kim
     
    Henry Wong
    author
    Sheriff
    Posts: 22542
    109
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It has already helped me on one thing and I was wondering if I could confirm this with you ? I was reading more about the Timer class and realized that I had misunderstood it. My one thread that has a constant while(true) loop will periodically need to start the new TimerTasks. I thought I needed to create a new Timer() object for each task, but if I am understanding correctly I can just make one Timer class, and then add the tasks to it as needed, thus cutting down on the number of threads greatly. Is that correct? The Timer object itself starts a thread, and then all the tasks will get handled by that one thread?


    Glad to hear that it helped ...

    Yes. You can "just make one Timer class, and then add the tasks to it as needed, thus cutting down on the number of threads greatly". And yes, "all the tasks will get handled by that one thread" of the java.util.Timer class.

    Keep in mind that the tasks must be short lived, since there is only one thread, if your tasks take too long to run, you can starve out the other tasks.

    Also, there may not be a need for your "thread that has a constant while(true) loop [which] will periodically need to start [or add] the new TimerTasks". If you are running the same timer task, at regular intervals, there is an overload schedule() method that lets you do this.

    Henry
     
    Enjoy the full beauty of the english language. Embedded in this tiny ad:
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!