sfgroups
sfgroups

Reputation: 19109

Micronaut Configure micronaut-jdbc-tomcat Data source for MySQL

In Development I am using below configuration to connect to MySQL database. I like to use Tomcat Data source for MySql Connection. Didn’t find example in the Micronaut document page.

What change required in application.yaml file to use Tomcat Datasource?

---
datasources.default: {}
---
hibernate:
  hbm2ddl:
    auto: update
  cache:
    queries: false
    use_second_level_cache: true
    use_query_cache: false
    region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
dataSource:
  url: jdbc:mysql://localhost:3306/db
  dbCreate: create-update
  pooled: true
  jmxExport: true
  driverClassName: com.mysql.cj.jdbc.Driver
  dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  username:  
  password:

Upvotes: 0

Views: 4034

Answers (2)

user4695271
user4695271

Reputation:

While configuring your data source in JPA, you can take advantage of Hikari; first you would need to import these:

implementation "io.micronaut.configuration:micronaut-hibernate-jpa"
implementation "io.micronaut.configuration:micronaut-jdbc-hikari"

I'm assuming you already have micronaut-hibernate-jpa, but I put it anyway. Additionally you can also add H2 as runtimeOnly (like runtimeOnly "com.h2database:h2") so it's easier to bootstrap and test the application.

After that, all you would need is to add these in your application.yml:

datasources:
  default:
    driverClassName: ${JDBC_DRIVER:org.h2.Driver}
    password: ${JDBC_PASSWORD:""}
    url: ${JDBC_URL:`jdbc:h2:mem:test_db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL`}
    username: ${JDBC_USER:sa}

...and you should be all set! An almost fully working example is here.

datasources is located at the root in application.yml, not as a child of any other key(s).

Last but not least, if you need to add more settings to tweak the data source configuration a little bit, you can reference these settings.

Upvotes: 2

user2809386
user2809386

Reputation:

you need mysql connector.

I am using this version.

compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.47'

see this site. If you are using Mysql 5.x you need 5.x's Drivers and using driverClassName: "com.mysql.jdbc.Driver". But if you are using Mysql 8.x , you need 8.x's Driver and using driverClassName: "com.mysql.cj.jdbc.Driver"

it depends on your Mysql version.

and you need one of this:

compile "io.micronaut.configuration:micronaut-hibernate-jpa"
compile "io.micronaut.configuration:micronaut-jdbc-hikari"

or you can use

compile "io.micronaut.configuration:micronaut-jdbc-tomcat"
compile "io.micronaut.configuration:micronaut-hibernate-jpa"

see this guide :

http://guides.micronaut.io/micronaut-data-access-jpa-hibernate/guide/index.html

Upvotes: 1

Related Questions