• Post Reply Bookmark Topic Watch Topic
  • New Topic

Oracle Abstration Class using Java  RSS feed

 
Ashley Shookhye
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I am currently writing an Oracle Abstraction class in Java to be used in my future applications. The methods inside the class are being declared as static.

Is that safe to do?

Thanks for your advise.

Rgds,
Ashley
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I work on the assumption that every occurrence of the keyword static must have a good explanation. Otherwise it is a serious error. Whether that is safe to do we can only tell you if you give us more details.
 
Ashley Shookhye
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you for your response.
Below is one of the methods I am using to insert into a table.


I am developing a JEE 7 application. If i am not mistaken, all instances would share the same method. My main worry is if several people are accessing the application and inserting into the table, would not that be a problem?

Thanks,
Ashley
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Databases are designed to handle multiple people inserting, updating, deleting at the same time, so that won't be an issue.
So long as your Connection isn't shared.

I have a couple of comments on that code, though.
There's no point using a PreparedStatement if you aren't going to bind the variables in and just concatenate the statement.

you should (probably) be building up a series of ? placeholders in your statement rather than the values, and then use another loop to call ps.setObject(<the value>).
Of course, actually knowing what the data types are would be even better, but that's the drawback of overly generic code like this.

Indeed, if any of these values are not numbers then that SQL is going to fail as there's no quotes anywhere.

The usual structure would be to have some form of DAO for each model entity, which would have specific INSERT/SELECT etc commands for that model/table mapping.
And that DAO would not use static methods.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Shookhye wrote:
I am developing a JEE 7 application. If i am not mistaken, all instances would share the same method. My main worry is if several people are accessing the application and inserting into the table, would not that be a problem?
Ashley

I'm assuming that you're developing something for work, not for school as homework or project for a grade.

There are various open source frameworks already developed, tested, and proven to be robust and up to the task of handling the demands of a typical enterprise application right out of the box. Why are you not looking at frameworks like Spring, Hibernate, and other ORM frameworks? These give you the plumbing code that you appear to be trying to write yourself.

In my opinion, that's like trying to build your own car from scratch. It's possible, given enough time, effort, and skill. But why go through all that trouble? A better alternative would be to leave the car building to people who have done it for a long time. Just go out and buy a car and start driving around in it. I would advise that you find a suitable framework, one that you would be reasonably comfortable using. The framework should take care of all the boilerplate, nitty-gritty, heavy lifting for you and leave you to worry more about the things that really matter in your application, like the domain logic.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Shookhye wrote:I am developing a JEE 7 application. If i am not mistaken, all instances would share the same method. My main worry is if several people are accessing the application and inserting into the table, would not that be a problem?


The important part is that those several people's requests are all in different threads. So as long as you keep your code thread-safe by only using local variables (not instance variables or static variables) and by ensuring that each thread has its own database connection, that's not a problem. To summarize, make sure that threads don't share data. Sharing code is fine.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The important part is that those several people's requests are all in different threads. So as long as you keep your code thread-safe by only using local variables (not instance variables or static variables) and by ensuring that each thread has its own database connection, that's not a problem. To summarize, make sure that threads don't share data

Just to add to that:

When you pass values along as parameters, methods that only deal with parameters and local variables are generally considered to be thread-safe. There is one caveat to "parameters are thread safe" though. You have to be careful with object references that are passed as parameters. You need to ensure that these references started off as local variables before they started getting passed around as parameters. If an object reference parameter can be traced back and found to be a reference to some object's instance or class variable, then that parameter MAY NOT BE thread safe. This can actually be the source of very insidious concurrency-related bugs. So it pays to know how your object reference parameters started their lives before they started getting passed around, whether as local variables (thread safe) or as instance or static variables (not thread safe).

The only way an object reference parameter can be guaranteed to be thread safe without regard to its "origin" is when that object is immutable - that is, when its state cannot be changed, as is the case with a String object, for example. Immutable objects are thread safe. This is why immutability is such a desirable characteristic of an object when it comes to concurrent programming and in functional programming constructs.

Parameters that are declared as primitive data types are always thread safe because of Java's pass-by-value design. That is, the parameter value is a copy of the original so it is essentially just a local variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!