The answer is no as expected and as it should be.
I know it's not wise to save network connections, but I was just checking if it is possible for driver writers to make Connection object serializable. The Connection's metadata interface does not provide any public method to retrieve password. It provides methods like getUsername() getURL() but no getPassword().
I may be wrong but isn't it doable by implementing readObject and reopening the connection ?
of course it couldn't be handled by Java, because the serialization mechanism doesn't know how (nor why) it should open the connection
you could imagine to extend your connection (if it's not final) , implement Serializable, and implement the readObject method
am I wrong ?
We can do it by implementing Externizable.
We can write via writeExternal() method as much info as we want to restore our object. i.e. URL, user name, password. All this info can be maintained in our Connection implementation. The readExternal() will read this info and can create live Connection object.
> We can do it by implementing Externizable.
> We can write via writeExternal() method as much info
> as we want to restore our object. i.e. URL, user
> name, password. All this info can be maintained in
> our Connection implementation. The readExternal()
> will read this info and can create live Connection
> object.
And why would you want to do that?
Hi Every one,
First of all, lets look at the purpose. what is that making you write an object which hold the Connection object to a network or to the file.
First check the condition. The conditions for serialization are
1) Connection object should not be declared as transiant.
2) And it should not be as static.
It the above conditions are meet then the it is serializable.
Now the obejct holding the connection object is written to the file or over the n/w.
Reading
Case 1 ) You read from the file at the server end no issues. You can reconstruct the Connection object and use it.
case 2) If you are writing to the n/w , your client is some where in Aus and the java server the db server are in US. Java Server being public and db server being local to the Java Server , how you would use the connection object to execute the querys on that DB.
Think about this :).
> First of all, lets look at the purpose. what is that
> making you write an object which hold the Connection
> object to a network or to the file.
> First check the condition. The conditions for
> serialization are
> 1) Connection object should not be declared as
> transiant.
> 2) And it should not be as static.
> It the above conditions are meet then the it is
> serializable.
> Now the obejct holding the connection object is
> written to the file or over the n/w.
>
> Reading
> Case 1 ) You read from the file at the server end no
> issues. You can reconstruct the Connection object and
> use it.
> case 2) If you are writing to the n/w , your client
> is some where in Aus and the java server the db
> server are in US. Java Server being public and db
> server being local to the Java Server , how you
> would use the connection object to execute the querys
> on that DB.
>
> Think about this :).
Hi,
Are n't you missing the whole point here? What you've said above is valid , of course, but then your readObject/writeObject methods would take care of restoring the connection object (from wherever) if such an implementation is taken up. IMHO, such an implementation is not practical in the first place, and if it's required in your project, it's bad design.
Regards
Hrishikesh
> Seriously, what do you think yourself?
>
> I mean come on......... *speechless*
>
> The short answer: no.
Then the short answer is wrong.
If the database connection is serializable, then by definition the answer is yes. Good luck finding a serializable database connection.
What you need to do is serialize all the informatoin necessary to reconnect to the database instead.