dimarts, 6 d’agost del 2019

Repository Spring

The use of @JPARepository or @CrudRepository is very handy, developers can do basic operations with no SQL operations. However it also hide the transaccion managemetn. By default every method has a transacciton scope. I have seen so many developers forget the use of @Transactional scope in their services. Hibernate and JPA force to you to create a Transaccion but with the use of frameworks it is deep hide and frecuantly ommited, skipped or simplely unknown by mediocre developers

Behaviour without Transaction management

the service produces different output depending of use of Transaccional scope

    @Transactional
    public void getGetOlder(int age, int plus) {

        List<User> listUsers = repoUser.findByAgeGreaterThan(age);
        for (User user : listUsers) {
            user.setAge(user.getAge() + plus);
            repoUser.save(user);
        }
    }

This give you a log with only one transaccion

2019-08-06 11:42:19,731 DEBUG [main] o.h.e.t.i.TransactionImpl [TransactionImpl.java:101] committing
2019-08-06 11:42:19,731 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:141] Processing flush-time cascades
2019-08-06 11:42:19,731 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:193] Dirty checking collections
2019-08-06 11:42:19,733 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:115] Flushed: 0 insertions, 2 updates, 0 deletions to 2 objects
2019-08-06 11:42:19,733 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:122] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2019-08-06 11:42:19,734 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:110] Listing entities:
2019-08-06 11:42:19,734 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:117] org.jordi.h2db.springboot.models.User{firstName=Joana, lastName=Fernandez, id=2, age=11}
2019-08-06 11:42:19,734 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:117] org.jordi.h2db.springboot.models.User{firstName=Rachel, lastName=Fernandez, id=1, age=11}
2019-08-06 11:42:19,735 DEBUG [main] o.h.SQL [SqlStatementLogger.java:94] update users set age=?, first_name=?, last_name=? where id=?
2019-08-06 11:42:19,739 DEBUG [main] o.h.SQL [SqlStatementLogger.java:94] update users set age=?, first_name=?, last_name=? where id=?
2019-08-06 11:42:19,759 DEBUG [Thread-3] o.h.i.SessionFactoryImpl [SessionFactoryImpl.java:792] HHH000031: Closing

whereras if you ommit the transaccion you will the below where you can see two commits instead of one.:

2019-08-06 12:00:19,763 DEBUG [main] o.h.e.t.i.TransactionImpl [TransactionImpl.java:101] committing
2019-08-06 12:00:19,764 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:141] Processing flush-time cascades
2019-08-06 12:00:19,765 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:193] Dirty checking collections
2019-08-06 12:00:19,766 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:115] Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
2019-08-06 12:00:19,767 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:122] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2019-08-06 12:00:19,767 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:110] Listing entities:
2019-08-06 12:00:19,768 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:117] org.jordi.h2db.springboot.models.User{firstName=Rachel, lastName=Fernandez, id=1, age=11}
2019-08-06 12:00:19,769 DEBUG [main] o.h.SQL [SqlStatementLogger.java:94] update users set age=?, first_name=?, last_name=? where id=?
2019-08-06 12:00:19,774 DEBUG [main] o.h.e.t.i.TransactionImpl [TransactionImpl.java:56] On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
2019-08-06 12:00:19,775 DEBUG [main] o.h.e.t.i.TransactionImpl [TransactionImpl.java:84] begin
2019-08-06 12:00:19,776 DEBUG [main] o.h.l.Loader [Loader.java:2269] Loading entity: [org.jordi.h2db.springboot.models.User#2]
2019-08-06 12:00:19,777 DEBUG [main] o.h.SQL [SqlStatementLogger.java:94] select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.first_name as first_na3_0_0_, user0_.last_name as last_nam4_0_0_ from users user0_ where user0_.id=?
2019-08-06 12:00:19,780 DEBUG [main] o.h.l.Loader [DelegatingBasicLogger.java:384] Result set row: 0
2019-08-06 12:00:19,781 DEBUG [main] o.h.l.Loader [Loader.java:1532] Result row: EntityKey[org.jordi.h2db.springboot.models.User#2]
2019-08-06 12:00:19,784 DEBUG [main] o.h.e.i.TwoPhaseLoad [TwoPhaseLoad.java:145] Resolving associations for [org.jordi.h2db.springboot.models.User#2]
2019-08-06 12:00:19,785 DEBUG [main] o.h.e.i.TwoPhaseLoad [TwoPhaseLoad.java:290] Done materializing entity [org.jordi.h2db.springboot.models.User#2]
2019-08-06 12:00:19,785 DEBUG [main] o.h.l.Loader [Loader.java:2298] Done entity load
2019-08-06 12:00:19,786 DEBUG [main] o.h.e.t.i.TransactionImpl [TransactionImpl.java:101] committing
2019-08-06 12:00:19,787 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:141] Processing flush-time cascades
2019-08-06 12:00:19,787 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:193] Dirty checking collections
2019-08-06 12:00:19,788 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:115] Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
2019-08-06 12:00:19,789 DEBUG [main] o.h.e.i.AbstractFlushingEventListener [AbstractFlushingEventListener.java:122] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2019-08-06 12:00:19,789 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:110] Listing entities:
2019-08-06 12:00:19,790 DEBUG [main] o.h.i.u.EntityPrinter [EntityPrinter.java:117] org.jordi.h2db.springboot.models.User{firstName=Joana, lastName=Fernandez, id=2, age=11}
2019-08-06 12:00:19,790 DEBUG [main] o.h.SQL [SqlStatementLogger.java:94] update users set age=?, first_name=?, last_name=? where id=?
2019-08-06 12:00:19,810 DEBUG [Thread-3] o.h.i.SessionFactoryImpl [SessionFactoryImpl.java:792] HHH000031: Closing

diumenge, 9 de juny del 2019

VAGRANT W10


It took me a lot of time to get over the below problem Raw-mode is unavailable courtesy of Hyper-V. (VERR_SUPDRV_NO_RAW_MODE_HYPER_V_ROOT).

to fix it   HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity\Enabled set to 0

dissabte, 23 de febrer del 2019

CDI in JEE-Jakarta EE part I

Dependence injection commontly CI or CD/CI or CDI is the framework for JEE for dependence injection. Very similar to Spring. It also was know as IoC or inversion of control. the point is that de dependeces are injected instead of created by new…​ Injected means that the container created and pass the reference…​. thats all…​.

Specification

In case of OpenLiberty is based in JBooss implementation http://weld.cdi-spec.org/

Features

Instalation

In CDI 1.0 was necessary a file META-INF/beans.xml but not in later version. This file also can be located in anohter places.

Scopes

CDI is based in a concept of container which is actually a on memory key-value data object database. this container or database is partitionated in scopes so a bean only can see its neigbourds in its scope. the scope come from applications scope

  • @RequestScoped

  • @SessionScoped

  • @ApplicationScoped

  • @ConversationScoped. This is similar to transacctional in jpa…​.

Names

El Names or @Name("myname") is a name to be referenced either JSP or JSF

Creation

The below sample create a Bean of type String…​ how does it solve conflicts?

    @Produces
    @Property("")
    String  valueofProperty(InjectionPoint injectionPoint){
        Property property = injectionPoint.getAnnotated().getAnnotation(Property.class);
        return "clave mas "+ property.value();
           }

Ambiguity.

if CDI is actually a key-value. how does it look up objects?. by the type. and the ambibuguity is solved with custom anotations or called @Qualifier.

@Qualifier
@Target({ ElementType.FIELD,ElementType.METHOD, ElementType.TYPE,ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Property {
    @Nonbinding
    String value() default "";

Use

The most commotn is by @Inject similar to @Autowired in Spring}

dilluns, 22 d’octubre del 2018

Instalation

browsesync is based on node/js platform so npm install -g browser-sync

Default running

browser-sync start --server

in my case to go fast i got an alias in my bashrc

alias bserve='browser-sync start --server' then to run for me is

$ bserve --file * -w

diumenge, 14 d’octubre del 2018

GIT most used comands

GIT log and info

set alias into .bashrc vimlog

alias gitlog='git log --all --oneline --graph'

to see diferences betwen branches write .. betwen branches

git diff branch_a..branch_b

GIT Commit

when I sure git commit -m -a "message" when not it allows check the diferences git add -p

GIT branch

create a branch called features git checkout -b features merge into branch into master git checkout master to go to master and the git merge features. if there is confilcs resolve or file to file editing one by one or use git mergetool it opens a nice tool based in vimlo git checkout --theirs or git checkout --ours in case you wan take iether feture or master

GIT rebase

Use rebase when you want get all the master o shared repo to you personal branch

dilluns, 13 d’agost del 2018

Testing With Gradle

Gradle has Junit out of the box, the reporting is not still so fancy as maven but it has the same functionality

Gradle JUnit 5

testCompile('org.junit.jupiter:junit-jupiter-api:5.2.0')
testCompile('org.junit.jupiter:junit-jupiter-params:5.2.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')

to run the test just execute gradle test but if you wish and specific test ie the whole class is still posssible with gradle test --tests org.jordiesc.BlogTest

dimecres, 4 de juliol del 2018

Update Ubuntu certificates

  • create a directory into /usr/local/share/ca-certificates mkdir new directory

  • copy the certificate directory file *.crt give permsissions and chmod 644 for the files

  • sudo update-ca-certificates

Get the certificates chain from the server

openssl s_client -connect HOST:PORTNUMBER -showcerts save the files as crt.