Winte Winte
Winte Winte

Reputation: 763

java: How to synchronize access to db?

I parse some stuff from a several urls and insert data in database. I want to do this now in parallel. I create a thread pool and run links in separate threads. But it isn't safe. I decided synchronized method that get data from web and save it to database like this:

            synchronized (this) {
                Parser parser = new Parser(link);
                feeds = parser.parse();
                model.insertFeeds(feeds, link);
            }

But this approach blocked access to web and database for other threads until current thread parse data and insert (this is 99% of his work), so it seems that they are perform more than one by one, not as a parallel.

Could you tell what is the better solution for things like this?

Upvotes: 1

Views: 1189

Answers (1)

Aravind Yarram
Aravind Yarram

Reputation: 80176

You should let the DB handle the concurrency for you and the way of controlling that is through JDBC transaction isolation levels

Upvotes: 5

Related Questions