Skip to content

Commit

Permalink
create transaction only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Athou committed Feb 23, 2015
1 parent cdcf81a commit 1d088c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/commafeed/backend/feed/FeedQueues.java
Expand Up @@ -14,17 +14,20 @@

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.hibernate.SessionFactory;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.UnitOfWork;
import com.commafeed.backend.model.Feed;

@Singleton
public class FeedQueues {

private SessionFactory sessionFactory;
private final FeedDAO feedDAO;
private final CommaFeedConfiguration config;

Expand All @@ -35,7 +38,8 @@ public class FeedQueues {
private Meter refill;

@Inject
public FeedQueues(FeedDAO feedDAO, CommaFeedConfiguration config, MetricRegistry metrics) {
public FeedQueues(SessionFactory sessionFactory, FeedDAO feedDAO, CommaFeedConfiguration config, MetricRegistry metrics) {
this.sessionFactory = sessionFactory;
this.config = config;
this.feedDAO = feedDAO;

Expand Down Expand Up @@ -67,7 +71,7 @@ public synchronized FeedRefreshContext take() {
FeedRefreshContext context = takeQueue.poll();

if (context == null) {
refill();
UnitOfWork.run(sessionFactory, () -> refill());
context = takeQueue.poll();
}
return context;
Expand Down
Expand Up @@ -10,13 +10,10 @@

import lombok.extern.slf4j.Slf4j;

import org.hibernate.SessionFactory;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.UnitOfWork;

/**
* Infinite loop fetching feeds from @FeedQueues and queuing them to the {@link FeedRefreshWorker} pool.
Expand All @@ -26,7 +23,6 @@
@Singleton
public class FeedRefreshTaskGiver implements Managed {

private final SessionFactory sessionFactory;
private final FeedQueues queues;
private final FeedRefreshWorker worker;

Expand All @@ -36,9 +32,8 @@ public class FeedRefreshTaskGiver implements Managed {
private Meter threadWaited;

@Inject
public FeedRefreshTaskGiver(SessionFactory sessionFactory, FeedQueues queues, FeedDAO feedDAO, FeedRefreshWorker worker,
CommaFeedConfiguration config, MetricRegistry metrics) {
this.sessionFactory = sessionFactory;
public FeedRefreshTaskGiver(FeedQueues queues, FeedDAO feedDAO, FeedRefreshWorker worker, CommaFeedConfiguration config,
MetricRegistry metrics) {
this.queues = queues;
this.worker = worker;

Expand Down Expand Up @@ -68,7 +63,7 @@ public void start() {
public void run() {
while (!executor.isShutdown()) {
try {
FeedRefreshContext context = UnitOfWork.run(sessionFactory, () -> queues.take());
FeedRefreshContext context = queues.take();
if (context != null) {
feedRefreshed.mark();
worker.updateFeed(context);
Expand Down

0 comments on commit 1d088c5

Please sign in to comment.