Project

General

Profile

Support #5139

Postgre query

Added by Marko Taht over 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
Start date:
07/28/2016
Due date:
% Done:

0%

Estimated time:

Description

As i understand from tutorials, all database actions happen within transaction. I have set up postgre db and connected it with Wt. I know the connection works because i used Wt to create the tables and filled one table with Wt. But now i need to get info from it and im getting read access violation. conn- was 0xCCCCCCCC. I understand that this means its a nullpointer. But i dont know where it comes from.

@

void Database::run() {

dbo::backend::Postgres postgres("user=postgres password=postgres port=5432 dbname=Maalid");

session_.setConnection(postgres);

session_.mapClass("Artwork");

session_.mapClass("Album");

session_.mapClass("Author");

session_.mapClass("Exhibition");

// session_.createTables();

// addAuthors();

}

dbo::collection< dbo::ptr > Database::getAuthors() {

dbo::Transaction trans(session_);

dbo::collection< dbo::ptr > authors = session_.find();

trans.commit();

return authors;

}@

#1

Updated by Koen Deforche over 6 years ago

  • Tracker changed from Bug to Support
  • Status changed from New to Resolved
  • Assignee set to Koen Deforche

Hey,

Your connection needs to have the same lifetime as the session, so should also be a member that you keep around next to session_.

Koen

#2

Updated by Marko Taht over 6 years ago

Does the same apply to transaction? "Dbo load(): no active transaction" and "Cannot map tables after schema was initialized". I have a form with combobox and im trying to get combobox selection from database. Im using the Model/View approach for the form(XML for layout, WFormModel for model and WTemplateFormView to but it all together)

dbo::collection< dbo::ptr > Database::getAuthors() {

dbo::Transaction trans(session_);

dbo::collection< dbo::ptr > authors = session_.find();

trans.commit();

return authors;

}

#3

Updated by Roel Standaert over 5 years ago

  • Status changed from Resolved to Closed

The transaction only needs to be alive as long as queries may be made to the database. In your case you need an active transaction when iterating over that collection. You could iterate over all authors, create a vector and return that if you don't need the transaction to stay alive:

dbo::vector< dbo::ptr<Author> > Database::getAuthors() {
  std::vector< dbo::ptr<Author> > result;
  dbo::Transaction trans(session_);
  dbo::collection< dbo::ptr<Author> > authors = session_.find<Author>();
  for (dbo::collection< dbo::ptr<Author> >::iterator = authors.begin();
       it != authors.end(); ++it) {
    result.push_back(*it);
  }
  return result;
}

Also available in: Atom PDF