Tomcat connection pool with both Shiro and Postgres - java

I'm trying to add some efficiency to my web app with Tomcat jdbc connection pool. Currently i have configured it to work with Shiro - it saves logins in Postgres db. But at the same time i have other part of my app that needs to work with Postgres db. Is it possible to configure only one pool to be available both for Shiro and my app?
Part of the shiro.ini about jdbc:
ds = org.apache.tomcat.jdbc.pool.DataSource
ds.driverClassName = org.postgresql.Driver
ds.username = shiroadmin
ds.password = adminshiro
ds.url = jdbc:postgresql://localhost:5432/cleb
jdbcRealm.dataSource = $ds
PS: i'm using tomcat pool as a maven dependency

Related

Is there any way to timeout slow queries on application server

I am using MySQL DB and spring boot 2.0.0 deployed on tomcat server, is there any way I can timeout slow queries from my application server.
In hikari Connection Pool there is a property - maxLifetimeMs which you can manipulate in two different ways:
Adding values in property file :
spring:
hikari:
max-lifetime: 840000
Changing in the config :
final HikariDataSource dataSource;
config.setMaxLifetime(840000);
This will have set the maximum time taken out by a query. No query can run beyond the duration set by you.
For identification of slow query you need to integrate any APM tool with your application server

Spring JDBC Hikari constantly logon logoffs in Oracle Database

I am using spring boot hikari db pool to connect to an Oracle database (19C).
Our properties are as below:
spring.datasource.app.hikari.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.app.hikari.minimumIdle=5
spring.datasource.app.hikari.maximumPoolSize=10
spring.datasource.app.hikari.idleTimeout=600000
spring.datasource.app.hikari.connectionTimeout=180000
spring.datasource.app.hikari.max-lifetime=60000
Our Database's AUD table is filling up due to constant logon logoff actions getting registered from the services every second.
I used solution from below question (to decrease minimumIdle and increase idleTimeout for long idle times), but it did not resolve the issue.
spring jdbc hikari connection pool - constantly logs on and off to database
Can somebody help in this case. Thanks in advance.
If you have not override datasource with different property name for hikaricp then use below properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.connection-timeout=180000
spring.datasource.hikari.max-lifetime=60000

Connection error while creating a basic SpringBoot app with MySQL

So i'm trying to create basic SpringBoot app with DB connectivity and here is my application.properties
spring.datasource.url=jdbc:mysql://sql.somehost.net:3306/thisismydbname
spring.datasource.username=sql9XXXXXX
spring.datasource.password=XXXXXXXX
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
I created a very basic SpringBoot app with Spring Data JPA and maybe Spring Web dependencies.
When I try to boot up the app, I get the following error
java.sql.SQLException: Access denied for user 'sql9XXXXXX'#'ip6x-xxx-xx-xxx.dc.dc.cox.net' (using password: YES)
When I try to connect to MySQL using MySQL Workbench externally this works fine. Also, it's very weird why it's trying to use my ISP Cox in the connection. I don't know how it picked up my ISP details. I do have the somehost.net where my MySQL is hosted.

SQL Azure and Connection Pooling

I have been searching high and low and gathered bits and pieces, apologies if this has already been answered elsewhere but I am unable to find it.
I am writing a web application in Java with Tomcat, and SQL Azure in the backend.
There are multiple servlets accessing the SQL Azure DB. I would like to use Connection Pools as managed by Tomcat 8.5
My application context.xml in META-INF is as follows:
<Context>
<Resource name="jdbc/sqlazure"
auth="Container"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
type="javax.sql.DataSource"
maxIdle="30"
username="[username]"
password="[password]"
url="jdbc:sqlserver://[database]:1433;database=BackInfo;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30"
removeAbandonedTimeout="30"
logAbandoned="true" />
</Context>
In the Java Code, I access the typical way:
InitialContext ic = new InitialContext();ds = (DataSource)ic.lookup("java:comp/env/jdbc/sqlazure");
try(Connection con = ds.getConnection())....
Everything seems to work, so let me confirm my understanding here:
I do not need to specify a separate web.xml since I'm using Tomcat 8.5. Correct ?
Azure will automatically create a pool when I connect in this manner. The number of connections in the pool etc cannot (do not need to?) be configured.
Before I realized I would have other servlets that need to access the database, I had one servlet directly creating a Datasource via SQLServerConnectionPoolDataSource and getting a connection from there. The documentation states:
SQLServerConnectionPoolDataSource is typically used in Java Application Server environments that support built-in connection pooling and require a ConnectionPoolDataSource to provide physical connections, such as Java Platform, Enterprise Edition (Java EE) application servers that provide JDBC 3.0 API spec connection pooling.
Does this mean that when I use SQLServerConnectionPoolDataSource directly to ask for a connection, it will check if Tomcat supports pooling and then is basically using JDBC mechanisms to create a pool of SQL Azure connections managed by Tomcat ?
When getting the DataSource via Tomcat JNDI lookup, using SQLServerDriver as specified in context.xml. When the web app starts up, it will check context.xml and use SQLServerDriver to connect to SQL Azure, check if pooling is supported, if yes then Tomcat is using the driver to automatically creating a connection pool DataSource that it returns ?
I also just thought of one other question. Would it make sense to have a Singleton DataSource class that returns a reference to the pooled connection DataSource, or would it be better to have each servlet lookup the datasource in its init() and store in a private variable ?
Thanks
Based on my understanding, the jdbc connection pool for SQL Server is created by Java Application, not Azure does. My suggestion is that you need to refer to the Tomcat offical documents below to understand the JNDI Resources & JDBC DataSource.
JNDI Resources: http://tomcat.apache.org/tomcat-8.5-doc/jndi-resources-howto.html
JDBC DataSource: http://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html

SQL JDBTempalate weblogic

I am executing SQLs via following code statement.
Application server is weblogic 12c
and Spring 3.1.1 API is used.
getJdbcTemplate().execute()...
I am wondering if getJdbcTemplate() returns DB connection itself or a reference to connection pool hosted at weblogic.
And if connection is closed after sql is executed?
If you define the datasource on Weblogic level and you reference that via a JNDI lookup in your spring configuration ==> will return a connection from the pool it was configured on Weblogic.
However with Spring you can configure your own pool without using Weblogic's one.
So depends on how you use it.
Cheers.

Categories