1

I've got an IncomingTrackHandlerFactory (ith) which hands out instances of IncomingTrackHandler. These instances implements AutoCloseable. The IncomingTrackHandler deals with the database, and is short lived. Every instance is just used for a couple of queries and then discarded.

I don't get why the first block of code does not work. Why does Java tell me that it "cannot find symbol" for ith? I'm simply just declaring ith before the try block to be able to also have the ith variable at hand if an exception is caugth and the database transaction must be rolled back.

What am I missing?


Non working code


Working code


SantiBailors
  • 1,596
  • 3
  • 21
  • 44
sbrattla
  • 5,274
  • 3
  • 39
  • 63
  • 1
    Note that it's not really *Netbeans*, but the *Java compiler* telling you that. – kryger Mar 13 '17 at 11:45
  • @kryger : yes - sorry for not being specific about that. It's not Netbeans fault, it was just my (bad) way of formulating the question. – sbrattla Mar 13 '17 at 11:52

2 Answers2

1

As far as I know, it's compulsory to declare the resource in the try-with-resources block as it's done in the second example.

From the documentation

Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

More info here: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

SCouto
  • 7,808
  • 5
  • 32
  • 49
  • Do you mean that I will have to instantiate using the constructor within the try() section, and not through a factory within the try() section to hand out an instance? – sbrattla Mar 13 '17 at 11:56
  • You can instantiate it the way you like,but it shold be declared and instantiated inside he try section. – SCouto Mar 13 '17 at 11:59
  • Alright, I was sure that I previously had done this. I assume I will have to go for a nested try-catch to be able to rollback the database transaction upon a failure inside the try section. – sbrattla Mar 13 '17 at 12:04
  • Hi, you may want to check this other post. It's somehow similar to what you need http://stackoverflow.com/questions/15761791/transaction-rollback-on-sqlexception-using-new-try-with-resources-block – SCouto Mar 13 '17 at 12:15
1

Your ith object is only visible inside your try block and is an instance of AutoCloseable. The instance is not visible outside, neither in catch, finally or catch block. The ith resource is closed automatically when an exception is thrown or the try block is completed. Although you can catch the exception itself, but can not operate on the ith object itself, only inside the try block.

To use the rollback function, you have to declare another try-catch block inside it. (Original code example omitted due source code is in image format)

try(Object<AutoCloseable> smth = source.get())
{
   try {
       // operate on smth
   } catch (Exception e)
   {
      smth.rollback();
   }
}

For more information, check https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Balazs Vago
  • 601
  • 5
  • 7