@@ -58,6 +58,8 @@ public class LeanplumEventDataManager {
5858 private static LeanplumDataBaseManager databaseManager ;
5959 private static ContentValues contentValues = new ContentValues ();
6060
61+ static boolean willSendErrorLog = false ;
62+
6163 /**
6264 * Creates connection to database, if database is not present, it will automatically create it.
6365 *
@@ -76,8 +78,7 @@ public static void init(Context context) {
7678 }
7779 database = databaseManager .getWritableDatabase ();
7880 } catch (Throwable t ) {
79- Log .e ("Cannot create database." , t );
80- Util .handleException (t );
81+ handleSQLiteError ("Cannot create database." , t );
8182 }
8283 }
8384
@@ -93,9 +94,9 @@ static void insertEvent(String event) {
9394 contentValues .put (COLUMN_DATA , event );
9495 try {
9596 database .insert (EVENT_TABLE_NAME , null , contentValues );
97+ willSendErrorLog = false ;
9698 } catch (Throwable t ) {
97- Log .e ("Unable to insert event to database." , t );
98- Util .handleException (t );
99+ handleSQLiteError ("Unable to insert event to database." , t );
99100 }
100101 contentValues .clear ();
101102 }
@@ -115,14 +116,14 @@ static List<Map<String, Object>> getEvents(int count) {
115116 try {
116117 cursor = database .query (EVENT_TABLE_NAME , new String [] {COLUMN_DATA }, null , null , null ,
117118 null , KEY_ROWID + " ASC" , "" + count );
119+ willSendErrorLog = false ;
118120 while (cursor .moveToNext ()) {
119121 Map <String , Object > requestArgs = JsonConverter .mapFromJson (new JSONObject (
120122 cursor .getString (cursor .getColumnIndex (COLUMN_DATA ))));
121123 events .add (requestArgs );
122124 }
123125 } catch (Throwable t ) {
124- Log .e ("Unable to get events from the table." , t );
125- Util .handleException (t );
126+ handleSQLiteError ("Unable to get events from the table." , t );
126127 } finally {
127128 if (cursor != null ) {
128129 cursor .close ();
@@ -143,9 +144,9 @@ static void deleteEvents(int count) {
143144 try {
144145 database .delete (EVENT_TABLE_NAME , KEY_ROWID + " in (select " + KEY_ROWID + " from " +
145146 EVENT_TABLE_NAME + " ORDER BY " + KEY_ROWID + " ASC LIMIT " + count + ")" , null );
147+ willSendErrorLog = false ;
146148 } catch (Throwable t ) {
147- Log .e ("Unable to delete events from the table." , t );
148- Util .handleException (t );
149+ handleSQLiteError ("Unable to delete events from the table." , t );
149150 }
150151 }
151152
@@ -161,13 +162,25 @@ static long getEventsCount() {
161162 }
162163 try {
163164 count = DatabaseUtils .queryNumEntries (database , EVENT_TABLE_NAME );
165+ willSendErrorLog = false ;
164166 } catch (Throwable t ) {
165- Log .e ("Unable to get a number of rows in the table." , t );
166- Util .handleException (t );
167+ handleSQLiteError ("Unable to get a number of rows in the table." , t );
167168 }
168169 return count ;
169170 }
170171
172+ /**
173+ * Helper function that logs and sends errors to the server.
174+ */
175+ private static void handleSQLiteError (String log , Throwable t ) {
176+ Log .e (log , t );
177+ // Send error log. Using willSendErrorLog to prevent infinte loop.
178+ if (!willSendErrorLog ) {
179+ willSendErrorLog = true ;
180+ Util .handleException (t );
181+ }
182+ }
183+
171184 private static class LeanplumDataBaseManager extends SQLiteOpenHelper {
172185 LeanplumDataBaseManager (Context context ) {
173186 super (context , DATABASE_NAME , null , DATABASE_VERSION );
0 commit comments