10 Ocak 2016 Pazar

Java Calendar&Date Comparison

Hello,

Last week, I struggled with date comparisons in Java.
Normally, as you know, java.util.Date and Calendar have necessary functions for comparing operations. But when we talk about Timezone, the issue gets complicated.

You may know, java.util.Date objects do not carry any Timezone information, so when you compare dates with different timezones, you should be careful. You may say that, you would prefer java.util.Calendar instance for this operation. Bingo! That's true, but not complete.

I explore something. You have two calendar instances and you set their timezones correctly. Everything is looking good. When you compare each other with Calendar's functions, i.e. "after", you may be shocked, it gives wrong result. Because, when you create a Calendar, it is created with default system timezone. And when you change its timezone, this does not change its milliseconds property.. Compare methods use milliseconds, so you get wrong answer. You should add the offset milliseconds to calendar's after setting timezone, what am I taking about is this,

(taken from this URL,
http://stackoverflow.com/questions/230126/how-to-handle-calendar-timezones-using-java)

// My default timezone is GMT+2
Date date = new Date();
// get offset for GMT+2
int offset = TimeZone.getDefault().getOffset(date.getTime());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+5"));
System.out.println(cal.getTime());
// get new offset for GMT+5
int newOffset = cal.getTimeZone().getOffset(date.getTime() - offset);
cal.add(Calendar.MILLISECOND, newOffset-offset);
System.out.println(cal.getTime());

Here, as you see, new Calendar object is created with GMT+5 timezone and its milliseconds are set properly. By this, you can use compare methods correctly.

Have a nice day !

6 Ekim 2015 Salı

Do synchnorized blocks or methods on Java prevent from multi thread access for every thread?

Hi,

As you read the question, I want to answer it quickly :)

Answer is, it depends. If the synchronized method is marked as "static", yes, it will work for every instances of class, because static methods belong to class, not instance.
For synchronized blocks, if they are in a static method, the same things go for them.

Otherwise, if the method is not static, it will work per instance, one's lock will not affect the other instance's synchronized block.

Have a nice day!

5 Ekim 2015 Pazartesi

Good Reference Sources for Migration of Commons HttpClient 3.x to HttpComponents HttpClient 4.x

Hi,

I had a task for replacing our Commons HttpClient 3.x dependency to HttpComponents HttpClient 4.x
As the latter one contains many changes over predecessor library, there are many things to be corrected at client side.
So, I used the sources below to accomplish my task, they were very useful, I recommend you to look at these if you have a task like mine :)

http://debuguide.blogspot.in/2013/01/quick-guide-for-migration-of-commons.html

http://www.codingpedia.org/ama/how-to-use-the-new-apache-http-client-to-make-a-head-request/

http://stackoverflow.com/questions/6024376/apache-httpcomponents-httpclient-timeout


Have a good day!

4 Haziran 2015 Perşembe

Search on a CLOB column on Oracle DB

Hi,

You can user instr function for searching on a CLOB column.

Ex:

select * from TABLE_NAME where instr(COLUMN_NAME,'SEARCH PARAM') > 0 order by process_date desc

Have a nice day!

11 Mayıs 2015 Pazartesi

Getting RollbackException in Transactional Methods

Hi,

Here is the second problem i have to deal with, RollbackException.
As I search on Google, if you get exception in a transactional method, even if you catch it and not throw, the transaction is marked as rollback only and you cannot commit anything in this transaction.
This may be annoying if you get an ignorable exception, for example NoResultException.
For overcome this, you should take some precautions with Transactional annotation, like this,

@Transactional(rollbackFor=MyException.class, noRollbackFor=MyException2.class)
Specifying rollback and no-rollback situations is the best way to handle this.

Have a nice day!

Link: http://stackoverflow.com/questions/19302196/transaction-marked-as-rollback-only-how-do-i-find-the-cause


About JPA NoResultException

Hi,

Today I looked for NoResultException, wondered when it may occurs, found that,

If you search record with getSingleResult, NoResultException is thrown, because, as written at stackoverflow,

"If getSingleResult() would return null, you could not tell whether the query did not match any row or whether the query matched a row but the selected column contains null as its value"

Otherwise, if you search with query, the return object will be null(or if you cast result to List, it will be empty), so you should deal with null or empty list in this situation.

Link: http://stackoverflow.com/questions/1579560/why-in-jpa-entitymanager-queries-throw-noresultexception-but-find-does-not

Have a nice day !

31 Ekim 2014 Cuma

JaxWS Client Üretme Çakışma Hataları

Merhabalar,

Java webservis client üretirken birçok zaman client üretmek bir kabus haline dönüşebiliyor. Farklı webservis implementasyonları farklı sonuçlar üretebiliyor, örneğin bir wsdl için axis ile üretilen client çalışırken diğeri için wsimport ile üretilen client başarılı olabilmektedir. Burada alınabilecek birçok çeşit hata vardır, authentication hatası, collision hatası, constructor hatası vs. Bu hataların çözümü için elbette hatanın kök nedenini iyi analiz etmek gerekiyor, bunun için tecrübe ile ya da google ile sonuç alınabiliyor :)

Benim bahsedeceğim hatayı almamın sebebi, bir sebeple client sınıflarını tek paket altında toplama ihtiyacım ve bu pakette farklı object factory'de olması gereken sınıfların aynı object factory'de çakışma yaşamasıdır. Hatanın kısaca açıklaması şu şekilde,

Two declarations cause a collision in the ObjectFactory class.

Bunu çözmek için, çakışan sınıflardan en az bir tanesi için binding tanımlayarak bunun object factory'deki metod ismine müdahale ediyoruz, pom'da binding tanımlanışı ve binding dosyasındaki tanımlamalar aşağıdaki gibidir.

pom.xml
...
..
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<target>2.1</target>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/binding.xml</bindingFile>
</bindingFiles>
</configuration>
<phase>generate-sources</phase>
</execution>
</executions>
..
...

binding.xml

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="1.0">

  <jaxb:bindings schemaLocation="https://dene.me.com/DenemeWS/DenemeService?xsd=3">
    <jaxb:bindings node="//xs:element[@name='Exception']">
      <jaxb:factoryMethod name="Exception2"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>


Herkese iyi çalışmalar dilerim ;)