Using yield to iterate over a datareader might not close the connection? - .net-2.0

Here is a sample code to retrieve data from a database using the yield keyword that I found in a few place while googling around :
public IEnumerable<object> ExecuteSelect(string commandText)
{
using (IDbConnection connection = CreateConnection())
{
using (IDbCommand cmd = CreateCommand(commandText, connection))
{
connection.Open();
using (IDbDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
yield return reader["SomeField"];
}
}
connection.Close();
}
}
}
Am I correct in thinking that in this sample code, the connection would not be closed if we do not iterate over the whole datareader ?
Here is an example that would not close the connection, if I understand yield correctly..
foreach(object obj in ExecuteSelect(commandText))
{
break;
}
For a db connection that might not be catastrophic, I suppose the GC would clean it up eventually, but what if instead of a connection it was a more critical resource?

The Iterator that the compiler synthesises implements IDisposable, which foreach calls when the foreach loop is exited.
The Iterator's Dispose() method will clean up the using statements on early exit.
As long as you use the iterator in a foreach loop, using() block, or call the Dispose() method in some other way, the cleanup of the Iterator will happen.

Connection will be closed automatically since you're using it inside "using" block.

From the simple test I have tried, aku is right, dispose is called as soon as the foreach block exit.
#David : However call stack is kept between call, so the connection would not be closed because on the next call we would return to the next instruction after the yield, which is the while block.
My understanding is that when the iterator is disposed, the connection would also be disposed with it. I also think that the Connection.Close would not be needed because it would be taken care of when the object is disposed because of the using clause.
Here is a simple program I tried to test the behavior...
class Program
{
static void Main(string[] args)
{
foreach (int v in getValues())
{
Console.WriteLine(v);
}
Console.ReadKey();
foreach (int v in getValues())
{
Console.WriteLine(v);
break;
}
Console.ReadKey();
}
public static IEnumerable<int> getValues()
{
using (TestDisposable t = new TestDisposable())
{
for(int i = 0; i<10; i++)
yield return t.GetValue();
}
}
}
public class TestDisposable : IDisposable
{
private int value;
public void Dispose()
{
Console.WriteLine("Disposed");
}
public int GetValue()
{
value += 1;
return value;
}
}

Judging from this technical explanation, your code will not work as expected, but abort on the second item, because the connection was already closed when returning the first item.
#Joel Gauvreau : Yes, I should have read on. Part 3 of this series explains that the compiler adds special handling for finally blocks to trigger only at the real end.

Related

How do I block the current thread until OnComplete has finished executing without the use of traditional threading primitives?

How do I block the current thread until the OnComplete handler of my observer has finished, without the use of threading primitives?
Here is my code. I want that the Console.WriteLine("Press... statement should be executed only after the OnComplete handler, namely ResetCount has finished executing.
class Program
{
private static long totalItemCount = 0;
private static long listCount = 0;
static void Main()
{
Console.WriteLine($"Starting Main on Thread {Thread.CurrentThread.ManagedThreadId}\n");
var o = Observable.Timer(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(1))
.Take(20)
.Concat(Observable.Interval(TimeSpan.FromSeconds(0.01)).Take(200))
.Buffer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
o.Subscribe(Print, onCompleted: ResetCount);
// How I make sure this line appears only after the OnComplete has fired?
// Do I have to use traditional threading primitives such as wait handles?
// Or just cause the main thread to sleep long enough? That doesn't seem right.
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
private static void ResetCount()
{
if (listCount > 0)
{
Console.WriteLine($"{totalItemCount} items processed in {listCount} lists.");
}
else
{
Console.WriteLine($"{totalItemCount} items processed.");
}
Interlocked.Exchange(ref totalItemCount, 0);
Interlocked.Exchange(ref listCount, 0);
}
static void Print<T>(T value)
{
var threadType = Thread.CurrentThread.IsBackground ? "Background" : "Foreground";
if (value is IList)
{
var list = value as IList;
Console.WriteLine($"{list.Count} items in list #{Interlocked.Increment(ref listCount)}:");
foreach (var item in list)
{
Console.WriteLine($"{item.ToString()}, ({threadType} #{Thread.CurrentThread.ManagedThreadId}), Item #{Interlocked.Increment(ref totalItemCount)}");
}
Console.WriteLine();
}
else
{
Console.WriteLine($"{value.ToString()}, ({threadType} #{Thread.CurrentThread.ManagedThreadId}), Item #{Interlocked.Increment(ref totalItemCount)}");
}
}
}
On Rx we have specific schedulers to handle threading, synchronization and related.
You can read more about that here:
http://www.introtorx.com/content/v1.0.10621.0/15_SchedulingAndThreading.html
But basically what you're looking for is changing this line:
.Buffer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), Scheduler.CurrentThread);
They're several ways to test/validate a Rx query. Keep in mind that wouldn't be the answer for all the problems.

Application.Load is not immediate

I have two functions:
private void Function1()
{
Function2();
// Do other stuff here which gets executed
}
private void Function2()
{
Application.LoadLevel("Level");
}
I had always lived in the thought that calling Application.LoadLevel() is immediate, but instead the other stuff in the Function1 does get executed.
Has this been changed in recent versions or has it always been there?
Application.LoadLevel is immediate in the sense that frames aren't generated until the level is loaded, but the current frame still ends.
This means that code register to execute will get executed.
It won't stop the current method from ending.
You could use coroutines to achieve that effect.
private void Function1()
{
StarCoroutine(Coroutine1());
}
private void Function2()
{
Application.LoadLevel("Level");
}
private IEnumerator Coroutine1()
{
Function2();
yield return null;
// Do other stuff here which gets executed
}

How to make multiple AsyncCallback calls in a Loop in GWTP?

This question is very useful. There're some questions about calling multiple AsyncCallback but they didn't tell how to call them in a loop.
Here is my problem. I am doing a project using Gwt-platform. I got a presenter TestPresenter.java which has these codes:
#Inject
DispatchAsync dispatchAsync;
private AsyncCallback<GetDataResult> getDataCallback = new AsyncCallback<GetDataResult>() {
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(GetDataResult result) {
// do Something o show the gui here
}
};
public void load_All_Gui_At_Once() {
for(int i=0; i<5; i++) {
GetData getDataAction=new GetData(i);
dispatchAsync.execute(getDataAction, getDataCallback);
}
}
The problem is that the program show the data but it showed in the wrong order. This is cos the next Async method started to run while the previous one has not finish yet.
Some suggested me to put the 2nd call inside onSuccess, but that is only for simple 2 sync calls. But in my case, I have to loop many unknown number of Async calls, then how can i do it?
This is a question similar to this one. All your calls are executed in the same instant, but the response time is unknown and it is not guaranteed the order. So the solution is almost the same, call the loop inside the callback. Your code should look like:
#Inject
DispatchAsync dispatchAsync;
private AsyncCallback<GetDataResult> getDataCallback = new AsyncCallback<GetDataResult>() {
int idx = 0;
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(GetDataResult result) {
if (result != null) {
// do Something or show the gui here
}
if (idx < 5) {
GetData getDataAction = new GetData(idx);
dispatchAsync.execute(getDataAction, getDataCallback);
}
idx ++;
}
};
public void load_All_Gui_At_Once(){
// Start the loop, calling onSuccess the first time
getDataCallback.onSuccess(null);
}

class member returns null after osgi bind method

My problem is that in the main class I have some osgi references that work just fine when the class is call. But after that all the references became null. When I close the main windows and call shutdown method, the hubService reference returns null. What do I do wrong here?
private void shutdown() {
if(hubService == null) {
throw new NullPointerException();
}
hubService.shutdownHub(); // why is hubService null?
}
// bind hub service
public synchronized void setHubService(IHubService service) {
hubService = service;
try {
hubService.startHub(PORT, authenticationHandler);
} catch (Exception e) {
JOptionPane.showMessageDialog(mainFrame, e.toString(), "Server", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
// remove hub service
public synchronized void unsetHubService(IHubService service) {
hubService.shutdownHub();
hubService = null;
}
If a field can be read and written by multiple threads, you must protect access to read as well as write. Your first method, shutdown, does not protect the read of hubService so that the value of hubService can change between the first read and the second read. You don't show the declaration of the hubService field. You could make it volatile or only read when synchronized (on the same object used to synchronized when writing the field). Then your shutdown implementation could look like:
private volatile IHubService hubService;
private void shutdown() {
IHubService service = hubService; // make a copy of the field in a local variable
if (service != null) // use local var from now on since the field could have changed
service.shutdownHub();
}
I assume your shutdown method is the DS deactivate method? If so, why do you shutdown in the unset method as well in the shutdown method?
Overall the design does not seem very sound. The IHubService is used as a factory and should return some object that is then closed in the deactivate method. You made the IHubService effectively a singleton. Since it must come from another bundle, it should handle its life cycle itself.
Since you also do not use annotations, it is not clear if your set/unset methods are static/dynamic and/or single/multiple. The following code should not have your problems (exammple code with bnd annotations):
#Component public class MyImpl {
IHubService hub;
#Activate
void activate() {
hubService.startHub(PORT, authenticationHandler);
}
#DeActivate
void deactivate() {
hubService.shutdown();
}
#Reference
void setHub(IHubService hub) { this.hub = hub; }
}

Java process problem in Eclipse

This is my code:
final String run_tool ="cmd.exe /C pelda.exe";
final Process tool_proc = null;
Runnable doRun = new Runnable() {
public void run() {
try {
tool_proc = Runtime.getRuntime().exec(run_tool);
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Thread th = new Thread(doRun);
th.start();
InputStream toolstr = tool_proc.getInputStream();
After the eclipse gives this warning message fot the tool_proc variable:
The final local variable tool_proc
cannot be assigned, since it is
defined in an enclosing type
I dont't know why my code doesn't work
please help me
The final modifier forbids changing the variable once it has been set. Kevin gives an excellent explanation of why you can't use it in this context.
The construction you have chosen for requires you to introduce a field in the parent class and set it via run():
class MyClass {
Process tool_proc = null;
void myFunction() {
final String run_tool ="cmd.exe /C pelda.exe";
Runnable doRun = new Runnable() {
public void run() {
try {
tool_proc = Runtime.getRuntime().exec(run_tool);
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Thread th = new Thread(doRun);
th.start();
// tool_proc will be null at this point!
InputStream toolstr = tool_proc.getInputStream();
}
}
In the above code I have demonstrated that tool_proc will always be null on the last line, as the thread has in all liklihood not finished its execution by this time!
You can expect NullPointerExceptions!
It seems what you are trying to do is very similar to this article about capturing application output asynchronously. It's a good read, and explains why conventional approaches can cause problems. I have an implementation very similar to their StreamGobbler (article page 4).
I know a good trick since i'm C++ coder and always want pointers in java :
final String run_tool ="cmd.exe /C pelda.exe";
final Process tool_proc[] = new Process[1];
Runnable doRun = new Runnable() {
public void run() {
try {
tool_proc[0] = Runtime.getRuntime().exec(run_tool);
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Thread th = new Thread(doRun);
th.start();
InputStream toolstr = tool_proc.getInputStream();
The tool_proc variable is final - final variables can only be assigned once, and you already assigned it the value null in the declaration. Removing the null assignment won't work because the runnable can be called more than once, resulting in multiple assignment. So your best option is to remove the final keyword and it should compile, at least
Edit
As noted in my comment to my answer, I neglected to account for the fact that tool_proc is a local variable, and without the final modifier it cannot be accessed from the anonymous Runnable subclass. As Paul first noted in his answer, you'll have to use a non-final field to store the Process object.
Initial value of final field cannot be changed. Remove final modificator from tool_proc:
Process tool_proc = null;