Use an sql class. Connection for each task or just a globally referenced one?

Asked

Viewed 93 times

0

I have a class that is responsible for verifying the user’s account and password when logging in, and in the future I will need to implement another class to get other data in the database and create a new connection.

It would be a good option to use only one class sql.Connection on the performance and readability of the code? Could lead to future problems?

  • Hello, Iago. Is this a simple academic project or a real application? In professional applications, connection management is usually delegated to a web container or framework. If it is a program made to learn, a simple way is to have a class that returns new connections to others. If you can give more details, it will be easier to help. Hug!

  • 1

    Hello. It’s both for learning and for a real @utluiz application. The application in the case is for android, cellular communication to sql server and vice versa.

  • At least you should comment on why my topic is closed, no return can do anything.

  • I did not vote to close, but I believe that the reason is that the details of the question are scattered by the comments and the question itself is wide, the tag about being for Android is missing, as it gives the impression that it can be for any Java program. If you want the question to be reopened, edit and add there the information that has been completed in the comments. In the OS, the question must contain all the necessary details.

  • 1

    I didn’t answer your question because I don’t work with Android, but reread now, I wanted to add one thing: keeping a connection open is not recommended simply because you will be consuming resources without need most of the time. In addition, the app can exit and return to memory at any time, so depending on where to store the object it can become "Stale" and at some point become unusable.

1 answer

4


Hello, young man!

The recommended is that you create a Factory of connections to the bank, a DAO (Data Access Object) to manipulate the access and acquisition of the information to the bank and, finally, one that allows you to validate whether the information is valid or not.

This way you will be able to maintain a high cohesion a low coupling.

See, in the highest layer, which I will call Validator, you receive your object with the user properties, such as login and password. In this Validator you request the information to the bank through the DAO - which I would recommend to be injected - and then homologate if the values are consistent or not. At the DAO you check which bank you should access, and then call your Factory. This can be by reading some system property, reading some external file, or even just asking another class.

Following this idea, if you have any problem in the validation, you only change in Validator; if you have problems in the query, change in DAO; finally, if it is a problem in the creation of the connection, change in Factory.

Any questions, just say the word!

Greetings

Edited

Here it is possible to find an example: Example

  • Correct me if I’m wrong to see if I understand, create a Factoryconnection class, which always gives me a connection when I need it, another class, in this case DAO, which will use this connection and transfer the necessary data, and one to validate information, right?

  • Exactly that, @Iagocoutinhocampos! I’ll try to make an example and pass it on to you. The idea is to separate the responsibilities. Na dao, you manipulate access to bank and research, for example. In Validator, you check if that information makes sense. Last but not least, in Factory vc creates your connection to the bank.

  • This would not generate many instances that would be used and then discarded leading to too much junk in memory?

  • 1

    Take a look here, @Iagocoutinhocampos. https://github.com/jeancopp/design-patterns-examples/tree/master/architecture-example As to this concern, it is sufficient that java is well configured to perform the cleaning. Memory consumption tends to become serrated, and from time to time will clean. If you use dependency injection, you already eliminate this, because CDI implementations will control this for you. spring, for example, vc can create as Singleton, so an instance is shared by the application as a whole.

  • Particularly my Daos receive a connection factory and I have only one instance of each DAO class. And they launch something like DataAccessException instead of SQLException, taking into account that the layer that makes use of the Daos should not know the implementation details of the lower layer (i.e., that the data comes from an SQL database).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.