Monday, September 21, 2020

Preparing Pivotal Spring Certification

I have bought "Core Spring 5 Certification in Detail" By: Ivan Krizsan on Kobo, only 5 USD and it's quite good. 

Previously I have tried the book by Iuliana Cosmina, but it's too verbose for my taste, I get lost in words, I like only very succinct statement with code samples where needed. Especially I hate "sample applications" developed for the sake of demonstrating the framework... normally they turn into overcomplicated examples where the same patterns are applied "ad nauseam".





Sunday, September 20, 2020

Viewing Spring transaction information

enable these flags: 

logging.level.org.springframework.orm.jpa=TRACE

logging.level.org.springframework.transaction=TRACE

 

 and you will see this in the logs: 

 

2020-09-20 18:40:00.788 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(239595802<open>)] for JPA transaction 

2020-09-20 18:40:00.794 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@12096038] 

2020-09-20 18:40:00.805 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [SessionImpl(239595802<open>)] for JPA transaction 

2020-09-20 18:40:00.805 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Participating in existing transaction 

2020-09-20 18:40:00.834 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit 2020-09-20 18:40:00.839 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [SessionImpl(239595802<open>)] 

2020-09-20 18:40:00.856 DEBUG 4904 --- [ restartedMain] o.s.orm.jpa.JpaTransactionManager : Closing JPA EntityManager [SessionImpl(239595802<open>)] after transaction 

 plus a lot of TRACE information equally useful (omitted here)

Demo code is available here https://github.com/vernetto/txdemo



Monday, August 10, 2020

Cookies sharing in Spring boot applications

    @GetMapping("/greeting")
    public String greeting(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        System.out.println(session);
        System.out.println(request);
        for (Cookie cookie : request.getCookies()) {
            System.out.println(cookie.getName() + " " + cookie.getValue());
        }
        Cookie uiColorCookie = new Cookie("color2", "red2");
        uiColorCookie.setDomain("localhost");
        uiColorCookie.setPath("/");
        response.addCookie(uiColorCookie);
        return "greeting";
    }

the code of the 2 applications is: 

https://github.com/vernetto/demosession01 

https://github.com/vernetto/demosession02 

http://localhost:8181/books01/greeting?name=pirla 

http://localhost:8282/books02/greeting?name=pirla 

In Firefox, use F12 and "storage" to examine cookies. 

Using embedded Tomcat, the session turns out to be:

org.apache.catalina.session.StandardSessionFacade https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/session/StandardSessionFacade.html