DataAdapters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All

I have a DataAdapter that I create in a middle data tier. Now if I called that middle data tier class and it creates that DataAdapter that is global in that class does it persist through the life of the App

Thanks

J
 
JJ,

What do you mean "it is global in that class"?

Just like any other object type, if the DataAdapter is declared as a *static* object of the class, after you create a DataAdapter and set the static object to the new created object (such as: myStaticDataAdapter = new DataAdapter(...);) it will persist through the life of the application, unless you explicitely remove it by setting the static object variable to null again.

And, just like any other object type, if the DataAdapter it is not declared static in the class, it will only persist untill that specific instance of the class which created it (such as: myOwnDataAdapter = new DataAdapter(...);) is destroyed.

--
Sorin Dolha [MCP, MCAD, MCSD]

Hi All,

I have a DataAdapter that I create in a middle data tier. Now if I called that middle data tier class and it creates that DataAdapter that is global in that class does it persist through the life of the App?

Thanks,

JJ
 
So if I call the data class's function and returns then is it destroyed on return or does the data class I created still exist for the life of the app?
 
JJ,

If your code is like this, the DataAdapter will persist after the call:

public class Data
{
private DataAdapter myDataAdapter = null;

public void CreateDataAdapter()
{
...
myDataAdapter = new DataAdapter(...);
...
}

public void OtherMethod()
{
if (myDataAdapter!=null)
{
... // Some code uses myDataAdapter
}
}
}

public class MainClass
{
public void Main()
{
Data d = new Data();
d.CreateDataAdapter();
d.OtherMethod(); // This will work, the DataAdapter still exists
// Here d is garbage-collected, therefore the DataAdapter dies too
}
}

If you make the DataAdapter static and the CreateDataAdapter method also static, you won't need to create a new Data object to perform the operations; and the DataAdapter will not be destroyed at the end of the caller method either (it will be garbaged collected only at the end of the application, unless you set it to null manually at some point).

I hope it helped.
 
Back
Top