Ir al contenido principal

Spring Boot - Migration / Data Source

Implement H2


In this case first will be use H2
  • Add H2 to pom.xml, in the dependencies section.
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

        <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

  • Modify application.properties file adding the following properties:

The files is in scr/main/resources
  • spring.h2.console.enabled=true
  • spring.h2.console.path=/h2

The value in path property is the URL to see H2 database console.
Ex. http://localhost:8080/h2/

Migration

Use Flyway

Add in pom.xaml the dependency

<dependency>
                 <groupId>org.flywaydb</groupId>
                 <artifactId>flyway-core</artifactId>

</dependency>
  • Create the folder in scr/resources
    • db -> 
      • migration

  • Add to the application.properties
    • flyway.baseline-description=true
  • spring.jpa.hibernate.ddl-auto=false;

  • Create file
    • V2_create_db.sql
Always start the file as V2__

Ex.

CREATE TABLE SHIPWRECK(
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(255),
   DESCRIPTION VARCHAR(2000),
   CONDITION VARCHAR(255),
   DEPTH INT,
   LATITUDE DOUBLE,
   LONGITUDE DOUBLE,
   YEAR_DISCOVERED INT

);

Datasource Configuration


Add in application properties:


spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

In this case the url is specifying that the db will be stored in a file


Other configurations:

spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.max-wait=10000
spring.datasource.min-evictable-idle-time-millis=1000
spring.datasource.min-idle=8

spring.datasource.time-between-eviction-runs-millis=1


Datasource for Spring

  • Create a folder for the configuration in
    • scr/main/java
      • <project>
        • config

  • Create a class as <Name>Configuration
  • Add above the class the annotation
    • @Configuration
  • Create a DataSource Bean 
    • @Bean
      @ConfigurationProperties(prefix="spring.datasource")
      @Primary
      public DataSource dataSource(){
      return DataSourceBuilder.create().build();
      }
  • Second Datasource
    • @Bean
      @ConfigurationProperties(prefix="datasource.flyway")
      @Primary
      public DataSource flyWayDataSource(){
      return DataSourceBuilder.create().build();
      }

  • The @Primary annotation is to avoid problems when many datasources are configured
  • The prefix is how is defined in the application.properties






Comentarios

Entradas populares de este blog

Android - Basic Steps (Service)

Service Run in the main thread of the hosting application Android can kill the Service if need the resources Purpose Supporting inter-application method execution Performing background processing Start a Service Call Context.startService(Intent intent)\ To call from a Fragment use getActivity().getApplicationContext().startService( intentService); Executing the service After call startService(...)  In the Service is executed the method onStartCommand(...) If the method returns the constant START_NOT_STICKY then Android will not restart the service automatically if the the process is killedp Foreground To execute the service foreground call the method startForeground() Use this if the user is aware of the process Bind to a Service Call the method Context.bindService( Intent service ServiceConnection con int flags ) Send Toast from the Service On the method onStartCommand receive the message   ...

Android - Basic Steps (Location & Maps)

Location Is composed by Latitude Longitude Time-stamp Accuracy Altitude Speed Bearing LocationProvider Types: Network  Wifi access points Cell phone towers GPS Passive Piggyback on the readings requested by other application Permissions Network  android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION GPS android.permission.ACCESS_FINE_LOCATION Passive Provider android.permission.ACCESS_FINE_LOCATION LocationManager System service for accessing location data getSystemService( Context.LOCATION_SERVICE ) Functions Determine the last known user location Register for location update Register to receive intents when the device nears or move away from a given geographic area LocationListener Defines callbacks methods that are called when Location or LocationProvider status change. Methods onLocationChanged(...) onProviderDisabled(...) onProviderEnabled(...) onStatusChan...

IIS - Permisions

IIS Permissions To enable the Active Directory connection in the IIS, follow the next steps: Go to IIS Go to Application Pool Select your App Pool Select Advanced Settings in the right side In the section Process Model Select in Identity value the property " NetworkService " You don´t need to restart your application