How can I retrieve latest from database using NHibernate after an update? - winforms

Here is the scenario:
I have a winforms application using NHibernate. When launched, I populate a DataGridView with the results of a NHibernate query. This part works fine. If I update a record in that list and flush the session, the update takes in the database. Upon closing the form after the update, I call a method to retrieve a list of objects to populate the DataGridView again to pick up the change and also get any other changes that may have occurred by somebody else. The problem is that the record that got updated, NHibernate doesn't reflect the change in the list it gives me. When I insert or delete a record, everything works fine. It is just when I update, that I get this behavior. I narrowed it down to NHibernate with their caching mechanism. I cannot figure out a way to make NHibernate retrieve from the database instead of using the cache after an update occurs. I posted on the NHibernate forums, but the suggestions they gave me didn't work. I stated this and nobody replied back. I am not going to state what I have tried in case I didn't do it right. If you answer with something that I tried exactly, I will state it in the comments of your answer.
This is the code that I use to retrieve the list:
public IList<WorkOrder> FindBy(string fromDate, string toDate)
{
IQuery query = _currentSession.CreateQuery("from WorkOrder wo where wo.Date >= ? and wo.Date <= ?");
query.SetParameter(0, fromDate);
query.SetParameter(1, toDate);
return query.List<WorkOrder>();
}
The session is passed to the class when it is constructed. I can post my mapping file also, but I am not sure if there is anything wrong with it, since everything else works. Anybody seen this before? This is the first project that I have used NHibernate, thanks for the help.

After your update, Evict the object from the first level cache.
Session.Update(obj);
Session.Evict(obj);
You may want to commit and/or flush first.

what about refresh? - see 9.2. Loading an object of the docs:
"sess.Save(cat);
sess.Flush(); //force the SQL INSERT
sess.Refresh(cat); //re-read the state (after the trigger executes)
"

Related

How to reload child view object completely in ADF

I have a 2 ViewObjects A and B. each connected with a ViewLink. I have shuttled the ViewLink in the ApplicationModule. So B comes under A in DataControl.
I have a Sunburst graph component using this instance. My requirement is to change the ViewObject Query at runtime. This works fine. But the graph is not getting refreshed for the level 2. Only Level 1 getting refreshed.
ie; A is only getting refreshed.
How to refresh B (Or the view link) ?
This sounds weird since view link usually get refreshed together automatically.
Your requirement is change the VO query at runtime. I don't have much details on your implementation, how would you manage to do this? Through an ApplicationModule method ? or some VO manipulation. Anyway, after you update a query, you need to do an executeQuery() right?
A possible way to solve your problem is to call B.executeQuery() after you update A's query.

Oracle ADF web browser refresh button gets old page

i'm developing an ADF application. I have a problem. My Jdev version 11.1.2.3.0.
So, For example i have an URL like this "http://www.hohoho.com/view/index.xhtml?_adf.ctrl-state=ju9lnu5ld_3"
In this page i have a table gets value from DB. For example again, i changed some rows in DB browser, and clicked the web browser's refresh button. But new results doesnt get!. For example i remove the "?_adf.ctrl-state=ju9lnu5ld_3" and enter url, this gets new results. How can handle this situation. I need that when an user clicked the refresh button, last result must be fetched. I think it is based on ADF state. How can handle this situation.
Solution
Thanks Andread, mysql's autocommit property default value is true, not false :) but i was using it false. I've solved the problem like yours, but my own solution so cool :) I've only overried the clearCache() method. And it solved the problem.
public void clearCache() {
getDBTransaction().commit(); // added
super.clearCache();
}
The results from the query are cached in the middle tier. This is the reason why updates to the database which are applied through a different channel than through your application are not reflected when the page reloads.
I am using JDeveloper 11.1.1.7. Setting the "CacheResults" property on the Iterator to false solved this issue (go to the "Bindings" tab of your page or page fragment which contains the table, select the Iterator Executable for your table data, and in the Property Inspector at the "Advanced" section, set "CacheResults" to "false").
In terms of XML, the iterator definition in the PageDef.xml file should look like
<iterator id="TestIterator" Binds="TestView1"
DataControl="AppModuleDataControl" RangeSize="25"
CacheResults="false"/>
There seem to be some additional approaches to solve this, probably this was necessary in earlier JDeveloper versions:
http://technology.amis.nl/2012/06/18/notifying-adf-applications-of-database-changes-fast-and-lean-using-database-query-result-change-notification-part-one/
http://radio-weblogs.com/0118231/stories/2005/06/16/whyIsntRefreshingTheBrowserPageEnoughToRefreshTheDataDisplayed.html
ADDENDUM 03-DEC-2012
OP uses MySQL. With MySQL, setting the CacheResults property is required, but not sufficient. By default, MySQL runs with autocommit=false which has the side effect of using the isolation level REPEATABLE READ. A SELECT implicitly opens a transaction, and subsequent SELECTs return the same result. The Oracle RDBMS uses READ COMMITTED by default, so that data inserted and committed in one session is returned by SELECT in a different session.
One solution to get around this in ADF is to create an implementation class for the View Object, override executeQueryForCollection() and commit the transaction before executing the query:
protected void executeQueryForCollection(Object object, Object[] object2, int i) {
getApplicationModule().getTransaction().commit();
super.executeQueryForCollection(object, object2, i);
}
Please use this carefully and review your actual isolation level requirements to make sure that you do not unintentionally commit data by a browser refresh. Another drawback of this solution is that it is not portable between Oracle RDBMS and MySQL.
See https://github.com/afester/StackOverflow/tree/master/AdfRefresh for an SSCCE.

How to force update a DB grid?

I may have been too clever for my own good :-/
I have a table which holds some pressure measurements. These are always stored as PSI, but the user can select a radio group button to toggle between PSI and BAR.
In order to keep the code clean and and push work onto the database, I created a second table for configuration items, with a single row. One column psi_bar_conversion will take the value either 1 or 14.5 as the user toggles the radio group.
In Delphi, my query which ties to my DB grid is set up with statements like
SELECT ROUND(inlet_waterPressure_psi /
(SELECT psi_bar_conversion FROM configuration),
(SELECT float_precision FROM configuration))
AS inlet_waterPressure,
FROM measurements
All of which works just fine (and perhaps I am explaining too much).
All that I am tring to do is add some code in the function which handles the radio button toggle to force my DB grid to refresh its contents becuase I have just updated the value of configuration.psi_bar_conversion (but no direct field of my query, nor of my datasource).
Should I invoke Refresh() or Invalidate() or SomeOtherFunction() - of the DB grid, the query, the datasrouce? That's what is confusing me.
Thanks in advance for any help ....
You need to close and then reopen the query to have the change in psi_bar_conversion and float_precision to take effect. The two sub-selects (for the values from configuration) only happen when the query is executed.
TDBGrid presentation depends on the connected TDataSet (via a TDataSource).
To update the Grid Values you have to refresh the data in TDataSet with the method TDataSet.Refresh.
To update a special Grid you can refresh the connected DataSet like this:
DBGrid1.DataSource.DataSet.Refresh;
But some TDataSet descendants will not refresh and that is documented by Embarcadero
TDataSet.Refresh
It depends on the components you use (i did a test with UniDAC and it works fine)
procedure TForm1.RadioGroup1Click( Sender : TObject );
var
LRate : Extended;
begin
case RadioGroup1.ItemIndex of
0 :
LRate := 1;
1 :
LRate := 14.5;
end;
UniConnection1.ExecSQL( 'UPDATE configuration SET psi_bar_conversion = :conversion', [LRate] );
DBGrid1.DataSource.DataSet.Refresh;
end;
If your components did not refetch the data on calling Refresh, then you have to close and reopen (also stated by documentation)
DBGrid1.DataSource.DataSet.Close;
DBGrid1.DataSource.DataSet.Open;
IMHO such components are not fully implemented, so this is just a workaround having side effects in calling some maybe unwanted events (BeforeClose, AfterClose, BeforeOpen, AfterOpen) which will not get fired using Refresh.
But at all it has nothing to do with SubSelects.
I'm afarid I can't help you with the Delphi side of things here.
On the datbase side of things....
Is there any reason that you can't just store both the bar and psi values in the database?
You could do the conversion when saving and then you are left just to do a simple select on the data when you want to view it. This could be done by the software that is performing the save or by a trigger in the database.
The reason I suggest this is that the psi-bar conversion ratio is not going to change so you are doing a bunch of processing everytime you view the data that is not required...

Problem with checking an original database record with an edited one

I am having problems saving database records using Linq in visual studio 2010 and sql server 2008.
My problem is that when I am editing some records I sometimes check the original database record for validation purposes, only the original entry seems to be updated in real time - I.e. it is already exactly the same as the edited record, before I have submitted the changes!
Could anyone suggest an effective method of coping with this? I have tried using a 2nd database connection or a 2nd data repository to call the original record from the db but it appears to be already changed when I debug it.
public void SaveobjectEdit(object objectToEdit)
{
object originalObject = GetobjectById(objectToEdit.Id);
if (originalObject.objectStatus != objectToEdit.objectStatus)
{
originalObject.objectStatus = objectToEdit.objectStatus;
}
SaveChanges();
}
The save changes just calls _db.SubmitChanges(); by the way
Has no one got any ideas for the above question?
I hope I was clear - for validation purposes I would like to compare an original database record with one that I am editing. The problem is that when I edit a record and then attempt to retrieve the original record before saving - the original record is exactly the same as the edited record.
If you're trying to retrieve the original record in code, from the same 'context' using the same access method, then it will contain the updated object. Rather than ask why you're doing this or what you're trying to achieve, I'll instead explain how I understand the data context / object context to work (in a very loose and vague fashion).
The context is something like an in-memory representation of your database, where everything is lazy-loaded. When you instantiate the context you're given an object which represents your data model (of course it may not be a 1-1 representation, and can contain various abstractions). Nothing is loaded into the context until necessary; any queries you write stay as queries until you peer in their results. When you access an item (e.g. GetobjectById(objectToEdit.Id)) the item is loaded into the context from the database and you can get and set its properties at your leisure.
Now, the important part: When you access an item, if it has already been loaded into the context then that in-memory object is returned. The context doesn't care about checking changes made; the changes won't be persisted to the database until you submit, but they remain in memory.
The way to refresh the in-memory objects is to call the Refresh method on the context. Try this test:
using (var db = new MyObjectContext())
{
var item = db.Items.First();
item.Name = "testing this thing";
Console.WriteLine(db.Shifts.First().Name);
db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.Items);
Console.WriteLine(db.Shifts.First().Name);
}
I believe this pattern makes a lot of sense and I'm not sure it could work any other way. Consider this:
foreach (var item in db.Items)
{
item.Name = "test";
}
Assert(db.Items.All(item => item.Name == "test"));
Would you want the Assert to fail? Should those items be reloaded? I don't believe so. I'm looking at the items in my context, not in the database. I'm not checking whether items in the database have been updated, but instead that I've updated all the items in the context of my code.
This is a good reason why I don't use MyObjectContext db - it is not a 'db' or a database connection. It's a context within which I can change whatever I want, so I name it such: MyObjectContext context.

How does the LINQ RefreshMode work?

In case of conflict, I need to overwrite the values in the database with my changes . I found the following article on MSDN that explains how to resolve conflicts using the RefreshMode:
http://msdn.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx
I decided KeepCurrentValues makes sense for my requirement and I also found this page with an example for this mode:
http://msdn.microsoft.com/en-us/library/bb399421.aspx
However when I implemented this the values from the database always overwrote my changes. I tried changing the RefreshMode to OverwriteCurrentValues and KeepChanges and each time the values from the database were saved. My approach was to manually change values in the database while in debug mode in VisualStudio. The code in VS is:
[MyDataContext] db = new [MyDataContext]();
try
{
[MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
old.IntField = 55;
// This is where I change the IntField value manually in the database in
// debug mode to generate the conflict.
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
}
I know that the conflict appears but every time, no matter how I change the RefreshMode, the value 55 is never saved and the changes that I made manually in the database are kept. Is there some trick to achieve the desired result? I have tried generating the conflict from inside the code at first and that didn't work as expected either. Maybe I didn't understand how the RefreshMode should work. Any ideas are welcome.
You just need to call SubmitChanges again...
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
db.SubmitChanges();
}
The conflict resolution method determines the state of the DataContext and not the underlying database.
KeepCurrentValues means keep all values as they are currently in the DataContext, which means the next call to SubmitChanges will save any changes made by the user, but it will also overwrite any changes made by other users after the data was loaded by the current user.
KeepChanges means keep only the values that have been changed since being loaded into the DataContext, which means the next call to SubmitChanges will save any changes made by the user and will preserve any changes made by other users. And, if another user changed the same value as the current user, the current user's change will overwrite it.
OverwriteCurrentValues means update the DataContext with the current database values, which means that all changes made by the current user will be discarded.
Before changing data, you refresh your object by overwriting from Db :
[MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
Db.Refresh(RefreshMode.OverwriteCurrentValues, old);
old.IntField = 55;
db.SubmitChanges(ConflictMode.ContinueOnConflict);

Resources