Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transacted part of the stream seem to affect non transacted parts #28

Open
Crystark opened this issue Jul 25, 2018 · 0 comments
Open

Transacted part of the stream seem to affect non transacted parts #28

Crystark opened this issue Jul 25, 2018 · 0 comments

Comments

@Crystark
Copy link

Crystark commented Jul 25, 2018

Hi @davidmoten

Being pleased with rxjava-jdbc I've been doing my latest app using rxjava2-jdbc. Thanks for bringing this to us! As always it's really helpful.

However, I've hit some issue when using transactions. I have the following code:

	@Override
	public Completable insertNewEntry(int id1, String id2, int id3, boolean active) {
		return db.update(INSERT_NEW_ENTRY)
			.parameters(id1, id2, id3, active)
			.transaction()
			.flatMapCompletable(tx -> tx.update(UPDATE_ITEM_REF)
				.parameters(id2, id1)
				.countsOnly()
				.ignoreElements());
	}

	@Override
	public Completable updateEntry(int id1, Date lastSyncDate, boolean active) {
		return db.update(UPDATE_ENTRY)
			.parameters(lastSyncDate, active, id1)
			.complete();
	}

Those methods are chained using compose and flatMaps in processing "pipeline" which goal is to keep data synced between multiple services.

My stream ends with a retry/repeat to make "daemon-like".

	/**
	 * Handles repeat and retry
	 */
	Flowable<?> handleRe(Flowable<?> f) {
		AtomicLong lastSubscribe = new AtomicLong();

		Function<? super Flowable<?>, ? extends Publisher<?>> re = o -> o
			.map(c -> {
				long sinceLasteRun = System.currentTimeMillis() - lastSubscribe.get();
				long nextRunIn = config.delayBetweenRunsMs() - sinceLasteRun;
				return nextRunIn;
			})
			.delay(l -> {
				Flowable<Object> s = Flowable.just(1);
				return l > 0 ? s.delay(l, TimeUnit.MILLISECONDS) : s;
			});

		return f
			.doOnSubscribe(s -> lastSubscribe.set(System.currentTimeMillis()))
			.doOnError(t -> L.warn("It went south!", t))
			.repeatWhen(re)
			.retryWhen(re);
	}

The thing is, updateEntry never affects the DB when insertNewEntry has been run. However if the entry is already inserted (after a repeat) and we only need to update, it works fine.

So I tried removing the transaction:

	@Override
	public Completable insertNewEntry(int id1, String id2, int id3, boolean active) {
		return db.update(INSERT_NEW_ENTRY)
			.parameters(id1, id2, id3, active)
			.counts()
			.flatMapCompletable(tx -> db.update(UPDATE_ITEM_REF)
				.parameters(id2, id1)
				.complete());
	}

And now updateEntry works one the first run when insertNewEntry is ran before.
Any idea what may be going on ?

Thanks alot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant