1
0
mirror of https://github.com/etesync/android synced 2025-06-26 09:52:38 +00:00

don't close httpClient while syncing

This commit is contained in:
rfc2822 2014-04-04 21:18:11 +02:00
parent 1e2051038c
commit f298d9dcb6

View File

@ -13,6 +13,7 @@ package at.bitfire.davdroid.syncadapter;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -42,7 +43,9 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
@Getter private static String androidID; @Getter private static String androidID;
protected AccountManager accountManager; protected AccountManager accountManager;
protected CloseableHttpClient httpClient;
protected CloseableHttpClient httpClient = DavHttpClient.create();
final ReentrantReadWriteLock httpClientLock = new ReentrantReadWriteLock();
public DavSyncAdapter(Context context) { public DavSyncAdapter(Context context) {
@ -54,7 +57,6 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
} }
accountManager = AccountManager.get(context); accountManager = AccountManager.get(context);
httpClient = DavHttpClient.create();
} }
@Override @Override
@ -64,8 +66,10 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
try { try {
httpClientLock.writeLock().lock();
httpClient.close(); httpClient.close();
httpClient = null; httpClient = null;
httpClientLock.writeLock().unlock();
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Couldn't close HTTP client", e); Log.w(TAG, "Couldn't close HTTP client", e);
} }
@ -90,6 +94,9 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
Log.i(TAG, "Nothing to synchronize"); Log.i(TAG, "Nothing to synchronize");
else else
try { try {
// prevent httpClient shutdown until we're ready
httpClientLock.readLock().lock();
for (Map.Entry<LocalCollection<?>, RemoteCollection<?>> entry : syncCollections.entrySet()) for (Map.Entry<LocalCollection<?>, RemoteCollection<?>> entry : syncCollections.entrySet())
new SyncManager(entry.getKey(), entry.getValue()).synchronize(extras.containsKey(ContentResolver.SYNC_EXTRAS_MANUAL), syncResult); new SyncManager(entry.getKey(), entry.getValue()).synchronize(extras.containsKey(ContentResolver.SYNC_EXTRAS_MANUAL), syncResult);
@ -115,6 +122,9 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
} catch (IOException ex) { } catch (IOException ex) {
syncResult.stats.numIoExceptions++; syncResult.stats.numIoExceptions++;
Log.e(TAG, "I/O error (Android will try again later)", ex); Log.e(TAG, "I/O error (Android will try again later)", ex);
} finally {
// allow httpClient shutdown
httpClientLock.readLock().unlock();
} }
} }
} }