Observability of the Java Virtual Machine

Image
The JVM is one of the most observable runtimes. It provides us lots of tools for troubleshooting a JVM application in production. 1. Thread observability Threads are how the JVM actually does work. When something is wrong in production, the symptom is almost always a thread: stopped, blocked, leaking etc. Thread dumps work on any JVM with no  instrumentation, no agents, no restarts. <Example project link with /threaddump endpoint>         // (1) Deadlock — two threads grab the same pair of locks in opposite order.         new Thread(() -> grab(LOCK_A, LOCK_B), "deadlock-A-then-B").start();         new Thread(() -> grab(LOCK_B, LOCK_A), "deadlock-B-then-A").start(); http://localhost:8080/actuator/threaddump To list the JVMS, we can use the command below. PS C:\observe-jvm> jps -lv 25296 jdk.jcmd/sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Microsoft\jdk-21.0.3.9-hotspot -Xms8m -Djdk.module.main=...

"Open Session In View" kalıbı

Bu kalıp Hibernate ile Java ağ uygulamaları geliştirirken ortaya çıkan bir probleme istinâden belirmiştir. Bir HTTP isteği web sunucusuna gelsin ve sunucumuzda yüklü olan kontrölcü servlet isteği ele alsın. Örneğin:

protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

String expenseId = request.getParameter("expenseId");

Expense expense = expenseService.loadWithId(expenseId);

return new ModelAndView("edit_expense", "expense", expense);

}

Burada expense nesnesi tembel bir şekilde elde edilmiş olabilir (lazily loaded). Bu durumda henüz ihtiyaç olmadığı için veritabanı tarafında bir SELECT sorgusu çalıştırılmış değildir. Fakat gösterim sayfasına (view) gönderdiğimiz nesnenin içindeki değerler, program akışı view kodlarına geldiğinde okunmak istenecektir. İşte burada bir sorun ortaya çıkıyor ki o da şu:

Bizim servis metodumuz yükleme (load) işlemini yaptıktan sonra Session nesnesini kapatacaktır. Böyle bir durumda view nesnesinin bizim expense varlığından değer okumaya çalışması "LazyInitializationException" ile sonuçlanır.

Bu sorunu çözmek için önerilen yol şudur:
Hibernate Session, sayfa gösterildikten sonra sonlandırılsın. Bunu gerçekleştirmek için Java Servlet Bildirgesinde yer alan "Filter" nesneleri kullanılır. Kendimiz bir Filter yazabileceğimiz gibi, Spring çatısının kullanıma sunduğu bir sınıfı da kullanabiliriz. Örneğin filter bildirimi web.xml içerisinde aşağıdaki biçimde yapılabilir:

< filter >
< filter-name > OpenSessionInViewFilter < /filter-name >
< filter-class >
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
< /filter-class >
< /filter >

< filter-mapping >
< filter-name > OpenSessionInViewFilter < /filter-name >
< servlet-name > myControllerServlet < /servlet-name >
< /filter-mapping >

Tabi ki Hibernate ayarları ile nesnelerin tembel olarak çekilmesini engelleyebiliriz. Fakat bunu her durumda uygulamak mümkün olmayabilir çünkü "lazy loading" bizi büyük performans kayıplarından kurtaran gerekli bir tekniktir. Hibernate gibi muazzam bir araç ile bu işleri çok ustaca yapmak kolay hale geliyor.

Comments

Popular posts from this blog

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Simplescalar Simulator - Part 2: sim-outorder.c

Notes on Java Performance