11package co .touchlab .dblocking ;
22
33import android .app .Activity ;
4+ import android .content .ContentValues ;
5+ import android .database .sqlite .SQLiteDatabase ;
6+ import android .os .AsyncTask ;
47import android .os .Bundle ;
58import android .os .Handler ;
69import android .util .Log ;
710import android .view .View ;
11+ import android .widget .EditText ;
812import android .widget .TextView ;
9- import com . example .R ;
13+ import co . touchlab .R ;
1014
1115import java .util .ArrayList ;
1216import java .util .List ;
@@ -18,6 +22,7 @@ public class MyActivity extends Activity
1822 private AtomicInteger allCount = new AtomicInteger ();
1923 private Handler uiHandler ;
2024
25+
2126 @ Override
2227 public void onCreate (Bundle savedInstanceState )
2328 {
@@ -63,6 +68,182 @@ public void onClick(View view)
6368 runAllThreads (allThreads );
6469 }
6570 });
71+
72+ findViewById (R .id .testTransactionIsolation ).setOnClickListener (new View .OnClickListener ()
73+ {
74+ public void onClick (View view )
75+ {
76+ DatabaseHelper helper = new DatabaseHelper (MyActivity .this );
77+ new SlowInsertThread (helper ).start ();
78+ new FastSelectThread (helper ).start ();
79+ }
80+ });
81+
82+ findViewById (R .id .noTrans ).setOnClickListener (new View .OnClickListener ()
83+ {
84+ public void onClick (View view )
85+ {
86+ runNoTrans ();
87+ }
88+ });
89+
90+ findViewById (R .id .trans ).setOnClickListener (new View .OnClickListener ()
91+ {
92+ public void onClick (View view )
93+ {
94+ runTrans ();
95+ }
96+ });
97+ }
98+
99+ @ SuppressWarnings ("unchecked" )
100+ private void runTrans ()
101+ {
102+ new AsyncTask (){
103+
104+ @ Override
105+ protected Object doInBackground (Object ... objects )
106+ {
107+ DatabaseHelper instance = DatabaseHelper .getInstance (MyActivity .this );
108+
109+ SQLiteDatabase writableDatabase = instance .getWritableDatabase ();
110+
111+ long start = System .currentTimeMillis ();
112+ writableDatabase .beginTransaction ();
113+ insertTest (instance );
114+ writableDatabase .setTransactionSuccessful ();
115+ writableDatabase .endTransaction ();
116+
117+ return start ;
118+ }
119+
120+ @ Override
121+ protected void onPostExecute (Object o )
122+ {
123+ showTime ((Long )o , DatabaseHelper .getInstance (MyActivity .this ));
124+ }
125+ }.execute ();
126+
127+
128+ }
129+
130+ @ SuppressWarnings ("unchecked" )
131+ private void runNoTrans ()
132+ {
133+ new AsyncTask (){
134+
135+ @ Override
136+ protected Object doInBackground (Object ... objects )
137+ {
138+ DatabaseHelper instance = DatabaseHelper .getInstance (MyActivity .this );
139+ long start = System .currentTimeMillis ();
140+ insertTest (instance );
141+
142+ return start ;
143+ }
144+
145+ @ Override
146+ protected void onPostExecute (Object o )
147+ {
148+ showTime ((Long )o , DatabaseHelper .getInstance (MyActivity .this ));
149+ }
150+ }.execute ();
151+ }
152+
153+ private void showTime (long start , DatabaseHelper instance )
154+ {
155+ long time = System .currentTimeMillis () - start ;
156+ ((TextView )findViewById (R .id .timeOut )).setText ("Total rows: " + instance .countSessions () +"/time: " + Long .toString (time ));
157+ }
158+
159+ private void insertTest (DatabaseHelper helper )
160+ {
161+
162+ int numberOfInserts = Integer .parseInt (((EditText ) findViewById (R .id .numberOfInserts )).getText ().toString ());
163+ while (numberOfInserts > 0 )
164+ {
165+ helper .createSession ("Count: " + numberOfInserts );
166+ numberOfInserts --;
167+ }
168+
169+ }
170+
171+ class SlowInsertThread extends Thread
172+ {
173+ private DatabaseHelper helper ;
174+ private Handler handler ;
175+
176+ SlowInsertThread (DatabaseHelper helper )
177+ {
178+ this .helper = helper ;
179+ handler = new Handler ();
180+ }
181+
182+ @ Override
183+ public void run ()
184+ {
185+ int count = 0 ;
186+ while (count < 10 )
187+ {
188+ SQLiteDatabase db = helper .getWritableDatabase ();
189+ db .beginTransaction ();
190+ final ContentValues contentValues = new ContentValues ();
191+
192+ contentValues .put ("description" , "asdlkfj" );
193+
194+ Log .i (getClass ().getName (), "insert" );
195+ db .insertOrThrow ("session" , null , contentValues );
196+ Log .i (getClass ().getName (), "start wait" );
197+ try
198+ {
199+ Thread .sleep (5000 );
200+ }
201+ catch (InterruptedException e )
202+ {
203+ e .printStackTrace ();
204+ }
205+ Log .i (getClass ().getName (), "end wait" );
206+ db .endTransaction ();
207+
208+ count ++;
209+ }
210+ }
211+ }
212+
213+ class FastSelectThread extends Thread
214+ {
215+ private DatabaseHelper helper ;
216+ private Handler handler ;
217+
218+ FastSelectThread (DatabaseHelper helper )
219+ {
220+ this .helper = helper ;
221+ handler = new Handler ();
222+ }
223+
224+ @ Override
225+ public void run ()
226+ {
227+ int count = 0 ;
228+ while (count < 100 )
229+ {
230+ helper .loadAllSessions ();
231+ Log .i (getClass ().getName (), "selected" );
232+
233+ Log .i (getClass ().getName (), "start wait" );
234+ try
235+ {
236+ Thread .sleep (500 );
237+ }
238+ catch (InterruptedException e )
239+ {
240+ e .printStackTrace ();
241+ }
242+ Log .i (getClass ().getName (), "end wait" );
243+
244+ count ++;
245+ }
246+ }
66247 }
67248
68249 private void runAllThreads (final List <DbInsertThread > allThreads )
0 commit comments