어떻게 해야 하나의 매끄러운 구성을 유지 테이블 사이션?
문제
경험이 어떤 문제를 설정하는 동안에 매끄러운 2.0.2.는 모든 구성에서 할 수있는 하나의 세션에서 손실됩니다.예를 들어,첫 번째 세션에서,저는 테이블을 만들고 추가 세 사람은:
// H2 in-memory database
lazy val db = Database.forURL("jdbc:h2:mem:contacts", driver="org.h2.Driver")
// Contacts table
lazy val contacts = TableQuery[ContactsSchema]
// Initial session
db withSession { implicit session =>
contacts.ddl.create
// Inserts sample data
contacts += Person("John", "123 Main street", 29)
contacts += Person("Greg", "Neither here nor there", 40)
contacts += Person("Michael", "Continental U.S.", 34)
// Successfully retrieves data
contacts foreach { person =>
println(person)
}
}
모두가 잘 이점이다.출력을 반복하고 세 사람이 내가 추가됩니다.시작할 때 새로운 세션을 시작했을 경험하는 문제입니다.
// New session in which the previous data is lost
db withSession { implicit session =>
contacts foreach { person =>
println(person)
}
}
위의 블록을 만듭니다 org.h2.jdbc.JdbcSQLException: Table "CONTACTS" not found
예외는 아니다.편집하면 다음과 같이
db withSession { implicit session =>
contacts.ddl.create
contacts foreach { person =>
println(person)
}
}
다음은 모든 데이터가 지워집니다.
에 나오는 것을 참조하십시오 Scalatra 가이드를 매끄러운 사용하는 유사한 구성의 것이 아니다.무엇이 잘못된 것입니까?어떻게 데이터를 얻을 지속하는 세션 사?다는 사실을 내가 사용하는 메모리에 데이터베이스는 아무것도 할까요?
해결책
두 가지 선택이 있습니다.
를 만들거나 세션이고 그것을 유지 열려 있습니다.을 수행할 수 있습 withSession 범위가 낮은에서 호출 스택 또는 db.createSession.
또는 추가 ;DB_CLOSE_DELAY=-1
데이터베이스에 url 이 있습니다.유지 db 한 살아있는 vm 을 실행됩니다.
보 http://www.h2database.com/html/features.html#in_memory_databases
제휴하지 않습니다 StackOverflow