3

Just try mongodb on Grails and I'm using mongodb:1.3.0 plugin

I have these domains :

class User {
    String username
    String passwordHash
    String email
    String pic
    static hasMany = [ roles: Role ]
    static constraints = {
        username(nullable: false, blank: false, unique: true)
        passwordHash(nullable: false)
        email(nullable: true, unique: true)
        pic(nullable: true)
    }
    static mapping = {
        username index: true
    }
}

and

class RespondentProfile {
    String id
    User userAccount
    List<RespondentGoldHistory> goldHistory = []
    String referer
    List<String> references = []
    long trust = 0
    long gold = 0

    static embedded = ["userAccount", "goldHistory"]

    static constraints = {
        referer (nullable: true)
    }
}

and I try to init them in BootStrap,

...
    def defaultUser = User.findByUsername('user123')?:new User(username: "user123", passwordHash: new Sha256Hash("password").toHex())
    defaultUser.addToRoles(surveyorRole)
    defaultUser.addToRoles(respondentRole)
    defaultUser.save()

    def foo = RespondentProfile.createCriteria().list {userAccount{eq 'username','user123'}}
    def defaultRespondentProfile = foo && foo.size() > 0 ? foo.get(0) : new RespondentProfile(userAccount: defaultUser)

    BasicDBList profileItems = (BasicDBList) com.mongodb.util.JSON.parse('[]')
    profileItems.add([code:'PI_ADDR001', value: 'nowhere']                            )
    profileItems.add([code:'PI_OCCUPATION001', value: 'jobless']                      )
    profileItems.add([code:'PI_COUNTRY001', value: 'ID']                              )
    profileItems.add([code:'PI_DOB001', value: Date.parse('dd/MM/yyyy', '30/06/1985')])
    profileItems.add([code:'PI_HEIGHT001', value: 179]                                )
    profileItems.add([code:'PI_WEIGHT001', value: 65]                                 )
    profileItems.add([code:'PI_EDU001', value: 'DEGREE']                              )
    profileItems.add([code:'PI_HOBBY001', value: 'Game']                              )
    defaultRespondentProfile['profileItems'] = profileItems

    defaultRespondentProfile.save()
...

after defaultRespondentProfile.save() I always hit this error :

Error 2013-07-07 12:15:09,708 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Could not commit Datastore transaction; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}; nested exception is com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}
Message: Could not commit Datastore transaction; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}; nested exception is com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}

find similar question here but I have no invalid chars for my fields name or keys

one more "weird" thing is the record is successfully stored when I do find

any ideas are appreciated :)

Community
  • 1
  • 1
  • 1
    not sure if this will solve your problem but try specifying write cocern static mapping = { writeConcern WriteConcern.JOURNAL_SAFE } – Ben W Jul 07 '13 at 18:59
  • Just curious, but what happens when you try to save a trivial domain class? I mean, create a User with one field, no relations, etc, and persist that in BootStrap. Does it also give an exception? – Erik Pragt Jul 07 '13 at 21:09
  • @ErikPragt It's Ok when I save a trivial domain class – Arnold Palar Jul 16 '13 at 02:18
  • @Saurabh I change my domain, make the profileItems as an embedded attribute of my RespondentProfile domain with type of Map and it works (which is previously like this before but I change it into dynamic attribute since I have a problem in creating criteria) – Arnold Palar Jul 16 '13 at 02:22

0 Answers0