0

업데이트 문제가 차단되었음을 발견했습니다. 데이터베이스는 항상 동일한 확장자로 생성되고 업그레이드되지만 닫히지는 않습니다. 이제는 "onblocked"함수가 호출됩니다.IndexedDB로 인해 데이터베이스 접근 불가 (차단 중), 차단 해제 방법이 있습니까?

현재 차단 된 데이터베이스의 차단을 해제하려면 어떻게해야합니까? 그리고 나는 이것을 어떻게 앞으로 막을 수 있습니까? 이것은 앱이므로 탭을 사용하고 있지 않습니다. 그리고 데이터베이스를 열어서 삭제할 수도 없으므로 (이것도 차단됩니다) 어떻게 닫을 수 있습니까?

(궁금 사람, 처음부터이 문제를 방지하기 위해, 당신은

색인화가 죽는 folllowing :

mydb.onversionchange = function(event) { 
    mydb.close(); 
}; 

원래의 게시물을 할을 가지고있는 경우 개방되지된다 나는 (실수로) 틀린 버전으로 열어 보려고한다. 내가 알 수있는 한, 최신 버전의 DB를 indexedDB에 요청할 방법이 없습니다. 따라서 다음 코드를 두 번 실행하려고하면 데이터베이스가 파괴되어 열리지 않습니다.

오류가 발생하지 않거나 오류가 발생하지 않습니다. 그냥 내 chrome.runtime.onInstalled.addListener에 자동으로

var db = null; 

//Note, no version passed in, so the second time I do this, it seems to cause an error 
var req = indexedDB.open("test"); 
req.onsuccess = function(event) { console.log("suc: " + event.target.result.version); db = event.target.result; }; 
req.onerror = function(event) { console.log("err: " + event); }; 
req.onupgradeneeded = function(event) { console.log("upg: " + event.target.result.version); }; 

//We're doing in interval since waiting for callback 
var intv = setInterval(
    function() 
    { 
     if (db === null) return; 

     clearInterval(intv); 

     var req2 = indexedDB.open("test", db.version + 1); 
     req2.onsuccess = function(event) { console.log("suc: " + event.target.result.version); }; 
     req2.onerror = function(event) { console.log("err: " + event); }; 
     req2.onupgradeneeded = function(event) { console.log("upg: " + event.target.result.version); }; 
    }, 
    50 
); 

그 코드의 모든 앉아있다. 그래서 내 앱을 업데이트하면 다시 호출됩니다. indexedDB.open("test")을 새 버전으로 전달하지 않고 setInterval 함수를 다시 실행하면 모든 것이 사용할 수 없게되고 "테스트"를 다시 열 수 없게됩니다. 데이터베이스를 열기 전에 데이터베이스의 버전에 대해 indexedDB를 쿼리하면 해결할 수 있습니다. 그게 존재합니까?

답변

1

아마도 도움이 될까요? 야그에 따라 확인

function getVersion(callback) { 
    var r = indexedDB.open('asdf'); 
    r.onblocked = r.onerror = console.error; 
    r.onsuccess = function(event) { 
    event.target.result.close(); 
    callback(event.target.result.version);  
    }; 
} 

getVersion(function(version) { 
    console.log('The version is: %s', version); 
}); 

,이 작은 폴더의 유틸리티 기능은 경로에 당신을 설정할 수 있습니다 :

var DATABASE_NAME_CONSTANT = 'whatever'; 

// Basic indexedDB connection helper 
// @param callback the action to perform with the open connection 
// @param version the version of the database to open or upgrade to 
// @param upgradeNeeded the callback if the db should be upgraded 
function connect(callback, version, upgradeNeeded) { 
    var r = indexedDB.open(DATABASE_NAME_CONSTANT, version); 
    if(upgradeNeeded) r.onupgradeneeded = updateNeeded; 
    r.onblocked = r.onerror = console.error; 
    r.onsuccess = function(event) { 
    console.log('Connected to %s version %s', 
     DATABASE_NAME_CONSTANT, version); 
    callback(event.target.result); 
    }; 
} 

// Now let us say you needed to connect 
// and need to have the version be upgraded 
// and need to send in custom upgrades based on some ajax call 

function fetch() { 
    var xhr = new XMLHttpRequest(); 
    // ... setup the request and what not 
    xhr.onload = function(event) { 
    // if response is 200 etc 
    // store the json in some variable 
    var responseJSON = ...; 

    console.log('Fetched the json file successfully'); 
    // Let's suppose you send in version and updgradeNeeded 
    // as properties of your fetched JSON object 
    var targetVersion = responseJSON.idb.targetVersion; 
    var upgradeNeeded = responseJSON.idb.upgradeNeeded; 

    // Now connect and do whatever 
    connect(function(db) { 
     // Do stuff with the locally scoped db variable 
     // For example, grab a prop from the fetched object 
     db.objectStore('asdf').put(responseJSON.recordToInsert); 

     // If you feel the need, but should not, close the db 
     db.close(); 
     console.log('Finished doing idb stuff'); 
    }, targetVersion, upgradeNeeded); 
    } 
} 
+0

결코 'onsuccess'를 호출하지 않습니다. 'onblocked' 만 호출하면 에러가 발생합니다 : DOM IDBDatabase Exception 11 –

+0

버전 정보를 전달하지 않아도 콘솔에 'blocked'오류가 출력됩니다. indexedDB.open에? – Josh

+0

또한 http://stackoverflow.com/questions/11898375/how-to-get-objectstore-from-indexeddb/16379163#16379163을 검토하십시오. 그것은 당신을 도울 수 있습니다. 전역 db 변수를 사용하지 마십시오. 콜백에서만 액세스 할 수 있습니다. 이렇게하면 차단 문제가 해결 될 것입니다.indexedDB 디자이너는 콜백 패턴을 사용하여 비동기 함수 호출을 허용하므로 전역 db 변수를 사용하려고하면 두통이 발생합니다. – Josh

0

나는 항상 버전 번호를 제공하는 것이 최선이라고 생각합니다. db 구조에서 업그레이드를 어떻게 관리 할 것인가? 당신이 좋은 기회를 가지지 않는다면, 클라이언트의 동일한 db 버전이 다른 데이터베이스 구조를 가지게 될 상황에 처하게 될 것이고, 나는 이것이 당신이 원하는 것이라고 생각하지 않는다. 그래서 변수에 버전 번호를 유지할 것을 제안합니다.

또한 indexeddb에서 작업 할 때 이전 버전에서 현재로 업그레이드 계획을 제공해야합니다. 의미 버전 4는 특정 구조를 가지고 있지만 버전 1,2 및 3에서와 동일한 구조를 처음부터 가져올 수 있어야합니다.

+0

과 같은 것 크롬을 다시 시작할 때 버전 번호를 저장하는 것이 좋습니다. –

+0

정적 변수로 하드 코드됩니다. var version = 3; –

+0

예, Chrome은 다시 시작 시마다 저장하지 않습니다. 애플리케이션이 서버에 연결되어 새로운 데이터와 데이터 스키마의 변경 사항을 확인할 수 있다고 상상해보십시오 (ajax를 통해). 발견되면 스키마가 변경되고 버전이 업데이트됩니다. 그 정보를 javascript의 변수에 저장하는 것은 크롬 재시작 사이에 존재하지 않습니다. 그럴거야? –

관련 문제