Wednesday, 21 August 2019

Is H2 Accessing filtered out???


2019-08-20 23:46:55.835  INFO 17304 --- [  restartedMain] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
2019-08-20 23:46:56.231  INFO 17304 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@626f6b95, org.springframework.security.web.context.SecurityContextPersistenceFilter@18dc3684, org.springframework.security.web.header.HeaderWriterFilter@c731e97, org.springframework.security.web.authentication.logout.LogoutFilter@70ed9554, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@332ce77b, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@27720351, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@70321b31, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@72cf2f4, org.springframework.security.web.session.SessionManagementFilter@600eb289, org.springframework.security.web.access.ExceptionTranslationFilter@b28bf68, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@783b78d]
2019-08-20 23:46:56.234  INFO 17304 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: Ant [pattern='/h2/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1bdf7eab, org.springframework.security.web.context.SecurityContextPersistenceFilter@737f3bd, org.springframework.security.web.header.HeaderWriterFilter@ecd60f, org.springframework.security.web.authentication.logout.LogoutFilter@13670ab, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@39a4d12b, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@27a48a12, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@411e6c47, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@32643e76, org.springframework.security.web.session.SessionManagementFilter@bc96433, org.springframework.security.web.access.ExceptionTranslationFilter@1aa74293, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6c7ee478]
2019-08-20 23:46:56.261  INFO 17304 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

Tuesday, 20 August 2019

How to setup H2 web console in SpringBoot

Springboot may auto-config H2 database, once it sees the dependency in the classpath.

How to set up the H2 database virtual console in Spring boot, to show H2 tables in a browser.

Go to the resource folder,  and add the following in the property file

spring.h2.console.enabled=true

thus h2 browser console will be enabled.

You may specify h2 console path
spring.h2.console.path=/h2

otherwise, the default will be /h2-console

spring-boot will config the database driver by default
Driver class: org.h2.Driver

You need to specify the JDBC URL for the project
spring.datasource.url= jdbc:h2:file:~/dev-db

Otherwise, the default value will be
JDBC URL:  jdbc url:  jdbc:h2:~/test

It may also specify username and password
spring.datasource.usename = xxx
spring.datasource.password = yyy

link to the URL: localhost:8080/h2

you will see the login console.

Click button 'connect', it shows the database management tool in the browser.






Sunday, 4 August 2019

Escaping refeences

Escaping references is a way to protect the data from being polluted.

fx: when returning a list of users from a class; even the list is encapsulated, but the reference to the list may be exposed to external after returning the same list to the external. On the case, the content may be modified by an external process.

There are two ways to escape the reference:
1. not returning an instance reference directly; fx. returning un-modified collections(immutable).
2. using the interface to customise the data model that is only readable.

How javax.inject.provider autowired in Spring

When moving a legacy code from the Guice to Spring boot, how to handle the injection of javax.inject.Provider ?  

Spring implements JSR 330 staring from Spring 3.0; the javax.inject annotations are supported too in the spring.

It seems that Autowire and Inject are doing the same in the spring;
So it looks that when auto-wiring a Provider, it should return a T type instance(scope = prototype? )

needs to go further to know


Spring - Using JSR 330 Provider to Inject Narrower Scoped Bean

Friday, 7 June 2019

Ubuntu Apt Command

Apt(Advanced Packaging Tool), it is used to install, upgrade, and remove a programme.

It needs a superuser right.

sudo apt update: updating the package database.

sudo apt upgrade: upgrading installed packages.

sudo apt install package-name: installing a new package.  

sudo apt install package-name-1 package-name-2 package-name-3: How to install multiple packages

sudo apt remove  package-name

sudo apt install package-name --reinstall: re-install installed package

sudo apt search search-item


Linux Package

Apt Command

Friday, 24 May 2019

Reusing Stream in Java 8


In java 8, once stream is terminated by a termination operation, then the stream cannot be re-used. Hence, you have to re-create the same stream again in some cases.

To overcome this drawback, before all termination operations, you may create a stream supplier followed by all intermediate operations. Thus, you may re-achieve the stream for different termination operations.

Each call to get() constructs a new stream.
Supplier<Stream<String>> streamSupplier =
    () -> Stream.of("d2", "a2", "b1", "b3", "c")
            .filter(s -> s.startsWith("a"));

streamSupplier.get().anyMatch(s -> true);   // ok
streamSupplier.get().noneMatch(s -> true);  // ok



Friday, 10 May 2019

Dead Lock due to Oracle Foreign Key not Indexed


(It comes from a product support task)

Checking the Oracle log file and find out the deadlock graph. 

[Transaction Deadlock]
The following deadlock is not an ORACLE error. It is a deadlock due to user error in the design of an application or from issuing incorrect ad-hoc SQL. The following information may aid in determining the deadlock:
Deadlock graph:
                                          ------------Blocker(s)-----------            ------------Waiter(s)------------
Resource Name                             process session holds waits serial  process session holds waits serial
TM-00017036-00000000-00000000-00000000        163     512    SX   SSX  62851     266     349    SX   SSX  13503
TM-00017036-00000000-00000000-00000000        266     349    SX   SSX  13503     163     512    SX   SSX  62851

The deadlock graph tells that the lock type is TM having a deadlock graph like 'TM SX SSX SX SSX', which suggest the likely deadlock type "Missing index on foreign key(FK) constraint"
The deadlock happens as delete an invoice. (A invoice has zero or many invoice lines.) 
{InvoiceLineToInvoice.java196, when InvoiceApi.deleteInvoice(invoice);}
OK; then mostly, my best guess according to the above, is due to the invoice line has been locked by a process.
I need a piece of evidence to approve this assumption.
as reading from the code, I have such an impression:
it seems that there are two threads or processes, Tr1 and Tr2;
Tr1 updates invoice line
Tr2 deletes the invoice.
An invoice contains many invoice-lines
Tr1 holds an invoice-line (L1).
Tr2 holds an invoice (I).
So when deleting an I (owned by tr1), whose operation cascades to its derivative table(L1);
when updating the L1, which operation backtrack to its parent (I);
and this may happen simultaneously.
These two processes should be wrapped in one transaction, and then it will be operated in sequential order, rather than a competition.

The Oracle log in production shows that: "The following deadlock is not an ORACLE error. It is a deadlock due to user error in the design of an application or from issuing incorrect ad-hoc SQL. The following information may aid in determining the deadlock "

The deadlock graph tells that the lock type is TM having a deadlock graph like 'TM SX SSX SX SSX', which suggest the likely deadlock type "Missing index on foreign key(FK) constraint"
The deadlock happens as delete an invoice.

OK; then mostly, my best guess according to the above, is due to the invoice line has been locked by a process.  I need evidence to approve this assumption.

I found something new, which exactly fits our case. It helps to understand Oracle deadlock graph.

I picked out the reason:
In short: Oracle doesn't create an index for the foreign, but on the primary key.

The detail:
Why unindexed foreign keys cause deadlocks
When you have an unindexed foreign key, Oracle acquires a full table lock on the child table when the primary key in the parent table is modified in some way. This could be as a result of updating the primary key or deleting a row, but not by inserting. The full table lock is acquired and released for every primary key modification that occurs, so if you are updating several rows in the table you will be locking/unlocking the table several times too. Whilst the table is locked, no other sessions may modify the child table, which is what causes the problem.
In conclusion, my recommendation (if you want to avoid deadlocks and performance hits) is to ALWAYS add an index when you create a foreign key.
the reference:
http://harrydoescode.co.uk/oracle-avoid-unindexed-foreign-keys/


the drawback of indexing foreign key: (1) When the table is small, it negatively reduces the performance due to extra indexing operations. for our case, invoice-line is a huge table, it will improve the performance. (2) Multiple foreign key indexing will slow down the insertion. for our case, I think it gives more benefits than the single drawback.




Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...