Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java t表,尝试使用此 db.beginTransactionNonExclusive(); try { //do some insertions or whatever you need db.setTransactionSuccessful(); } finally { db.endTransaction(); }_Java_Android_Multithreading_Sqlite_Concurrency - Fatal编程技术网

Java t表,尝试使用此 db.beginTransactionNonExclusive(); try { //do some insertions or whatever you need db.setTransactionSuccessful(); } finally { db.endTransaction(); }

Java t表,尝试使用此 db.beginTransactionNonExclusive(); try { //do some insertions or whatever you need db.setTransactionSuccessful(); } finally { db.endTransaction(); },java,android,multithreading,sqlite,concurrency,Java,Android,Multithreading,Sqlite,Concurrency,我尝试使用db.beginTransaction,但它锁定了bd最简单的方法:不要使用两个线程访问db。。。但我想这不是一个选项,真的。是的。但在我的live应用程序中,两个线程同时运行。所以我必须创建演示应用程序并尝试解决问题。但问题仍然存在。请给我一些其他解决方案尝试使您的DB对象挥发,特别是一个无用的方法?最简单的方法:不要使用两个线程访问DB。。。但我想这不是一个选项,真的。是的。但在我的实时应用程序中,两个线程同时运行。所以我必须创建演示应用程序并尝试解决问题。但问题仍然存在。请给我一

我尝试使用db.beginTransaction,但它锁定了bd

最简单的方法:不要使用两个线程访问db。。。但我想这不是一个选项,真的。是的。但在我的live应用程序中,两个线程同时运行。所以我必须创建演示应用程序并尝试解决问题。但问题仍然存在。请给我一些其他解决方案尝试使您的DB对象挥发,特别是一个无用的方法?最简单的方法:不要使用两个线程访问DB。。。但我想这不是一个选项,真的。是的。但在我的实时应用程序中,两个线程同时运行。所以我必须创建演示应用程序并尝试解决问题。但问题仍然存在。请给我一些其他解决方案尝试使您的DB对象挥发,特别是一个没有帮助的对象?setLockingEnabled(布尔lockingEnabled)在API级别16I中被弃用,我也面临类似的问题。如果Sqlite默认以序列化模式运行,那么为什么会出现这种锁定异常,因为即使我使用多个线程,Android本身也会处理所有操作?我的助手类也使用单例模式。setLockingEnabled(boolean lockingEnabled)在API级别16中被弃用,我也面临类似的问题。如果Sqlite默认以序列化模式运行,那么为什么会出现这种锁定异常,因为即使我使用多个线程,Android本身也会处理所有操作?我的助手类也在使用单例模式。你能详细说明一下这是如何解决问题的吗?你能详细说明一下这是如何解决问题的吗?
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new DbHelper(this);

        setContentView(R.layout.activity_main);
        try {
            dbHelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    Log.i("data", "webAllBrokers thread start");

                    GetAllBrokers(getApplicationContext());
                    // GetAllBrokers(getApplicationContext());
                    Log.i("data", "webAllBrokers thread complete");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();
        new Thread(new Runnable() {

            @Override
            public void run() {
                Log.i("data", "SelectAllData thread start.");
                SelectAllData();
                Log.i("data", "SelectAllData thread complete");
            }

        }).start();

    }

    private void SelectAllData() {
        // TODO Auto-generated method stub
        for (int i = 0; i < 100; i++) {
            TblBroker.SelectAll(dbHelper);
        }

    }

    public static void GetAllBrokers(Context context) throws IOException,
            XmlPullParserException, JSONException {
        // webservice call for retrieve data
        String result = GetAllBrokers(context, "Admin001");
        ArrayList<ModelBroker> lstBrokers = JsonParserGetAllBrokers
                .parserString(result);

        TblBroker.deleteAll(dbHelper);

        TblBroker.insert(dbHelper, lstBrokers);

    }
public class DbHelper extends SQLiteOpenHelper {


    // The Android's default system path of your application database.
    private static String PACKAGENAME = "com.example.webservicedemocallbackgroundwebservice";
    private static String DB_PATH = "/data/data/" + PACKAGENAME + "/databases/";
    private static String DB_NAME = "GRSL_Sales_DB.sqlite";
    public static int DELETED = 1;
    public static int UPDATED = 2;
    public static int NEW_RECORD = 3;
    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Lock r = rwl.readLock();
    private final Lock w = rwl.writeLock();
    private SQLiteDatabase myDataBase;

    private final Context myContext;
    private String TAG = "DbHelper";

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public DbHelper(final Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public final void createDataBase() throws IOException {

        final boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {
            // do nothing - database already exist
        } else {
            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            // db_Read = this.getReadableDatabase(DB_Internal);
            db_Read = this.getReadableDatabase();
            db_Read.close();

            copyDataBase();

        }
    }

    /**
     * Restore whole database without any data
     * 
     * @throws IOException
     */
    public final void RestoreDatabase() throws IOException {
        SQLiteDatabase db_Read = this.getReadableDatabase();
        db_Read.close();

        copyDataBase();
        Log.i(TAG, "Database REstored");
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {
        final File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * 
     * @throws IOException
     * */
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        final InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        final String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        final OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        final byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public final synchronized SQLiteDatabase openDataBase() {
        // Log.i("data", "data openDataBase");
        // Open the database
        final String myPath = DB_PATH + DB_NAME;
        Log.i("database", "data openDataBase" + myDataBase);

        if (myDataBase == null) {
            Log.i("database", "data openDataBase");
            myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
            myDataBase.beginTransaction();
        } 
        return myDataBase;
    }

    @Override
    public final synchronized void close() {
        // Log.i("data", "data closeDataBase");

        if (myDataBase != null) {
            Log.i("database", "data closeDataBase");
            myDataBase.setTransactionSuccessful();
            myDataBase.endTransaction();
            myDataBase.close();
            myDataBase = null;
        }
        super.close();
    }

    @Override
    public void onCreate(final SQLiteDatabase arg0) {
    }

    @Override
    public void onUpgrade(final SQLiteDatabase arg0, final int arg1,
            final int arg2) {
    }
}
public class TblBroker {
    public static final String TABLENAME = "Broker";
    public static final String ID = "ID";
    public static final String SAPCODE = "SAPCode";
    public static final String DISCRIPTION = "Discription";
    public static final String ISACTIVE = "IsActive";
    public static final String ISDELETED = "IsDeleted";
    public static final String City = "City";

    /**
     * 
     * SelectAll
     * 
     * @param dbaConnection
     * @return ArrayList<ModelBroker>
     */
    public static ArrayList<ModelBroker> SelectAll(DbHelper dbaConnection) {
        Log.i("data", "SelectAll start");
        ArrayList<ModelBroker> broker_aList = new ArrayList<ModelBroker>();

        SQLiteDatabase sqldb = dbaConnection.openDataBase();
        Cursor cursor = sqldb.rawQuery("Select * From " + TblBroker.TABLENAME,
                null);
        if (cursor != null && !cursor.isClosed())// If CursorBroker is null then
                                                    // do
        // nothing
        {
            if (cursor.moveToFirst()) {
                do {
                    // Set broker information in model.
                    ModelBroker modelBroker = new ModelBroker();
                    modelBroker.setID(cursor.getInt(cursor
                            .getColumnIndex(TblBroker.ID)));
                    modelBroker.setSAPCode(cursor.getString(cursor
                            .getColumnIndex(TblBroker.SAPCODE)));
                    modelBroker.setDiscription(cursor.getString(cursor
                            .getColumnIndex(TblBroker.DISCRIPTION)));
                    modelBroker.setIsDeleted(cursor.getString(cursor
                            .getColumnIndex(TblBroker.ISDELETED)));
                    modelBroker.setIsActive(cursor.getString(cursor
                            .getColumnIndex(TblBroker.ISACTIVE)));
                    modelBroker.setCity(cursor.getString(cursor
                            .getColumnIndex(TblBroker.City)));
                    broker_aList.add(modelBroker);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
        Log.i("data", "SelectAll end");
        dbaConnection.close();
        return broker_aList;
    }

    public static long insert(DbHelper dbaConnection,
            ArrayList<ModelBroker> listbroker) {

        long id = 0;
        SQLiteDatabase sqldb = dbaConnection.openDataBase();

        for (int i = 0; i < listbroker.size(); i++) {

            ModelBroker broker = listbroker.get(i);
            ContentValues values = new ContentValues();
            values.put(TblBroker.SAPCODE, broker.getSAPCode());
            values.put(TblBroker.DISCRIPTION, broker.getDiscription());
            values.put(TblBroker.ISACTIVE, broker.getIsActive());
            values.put(TblBroker.ISDELETED, broker.getIsDeleted());
            values.put(TblBroker.City, broker.getCity());
            Log.i("data", "sqldb.isOpen()=" + sqldb.isOpen());
            id = sqldb.insert(TblBroker.TABLENAME, null, values);
        }

        Log.i("data", "1 st loop end");
        Log.i("data", "2nd  loop start");
        for (int i = 0; i < listbroker.size(); i++) {
            ModelBroker broker = listbroker.get(i);
            ContentValues values = new ContentValues();
            values.put(TblBroker.SAPCODE, broker.getSAPCode());
            values.put(TblBroker.DISCRIPTION, broker.getDiscription());
            values.put(TblBroker.ISACTIVE, broker.getIsActive());
            values.put(TblBroker.ISDELETED, broker.getIsDeleted());
            values.put(TblBroker.City, broker.getCity());
            id = sqldb.insert(TblBroker.TABLENAME, null, values);
        }

        Log.i("data", "2nd loop end");
        // Insert
        // query
        // of
        // SQLiteDatabase
        // class.
        dbaConnection.close();
        return id;

    }

    public static int deleteAll(DbHelper dbaConnection) {
        SQLiteDatabase sqldb = dbaConnection.openDataBase();
        int row = sqldb.delete(TABLENAME, null, null);// Delete

        sqldb.close();
        sqldb = null;
        return row;
    }

}
   db.beginTransactionNonExclusive();
   try {
       //do some insertions or whatever you need
       db.setTransactionSuccessful();
   } finally {
       db.endTransaction();
   }