how to connect to hsqldb which is instantiated through spring - java

I'm using Embedded database for running my test cases while maven test phase. I assume Spring must be starting the HSQLDB server. Is my assumption correct ?
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:data.sql"/>
</jdbc:embedded-database>
How can i view the schema/table created in hsqldb using above mentioned script ? I tried connecting through DBVisulaizer but i don't see tables there.

The jdbc:embedded-database URL connects to an in-process memory database, not a server.
You need to start a server separately (a separate process) which fronts an in-memory database. You can find information on how to do this in the HSQLDB Guide.
http://www.hsqldb.org/doc/2.0/guide/listeners-chapt.html
You than use a normal connection URL such as jdbc:hsqldb:hsql://localhost/test in Spring to connect to the server. You can also connect to the server with DbVisualiser using the same URL.

Related

Analyzing the performance of SQL queries between OnPrem and AWS environment

I am working in modernizing an existing REST services and moving from OnPrem to AWS.
The legacy application
deployed in a OnPrem Liberty Server
MS SQL server DB was used.
Spring/Hibernate was the core technologies.
New Application Details :
Spring Boot deployed in the AWS environment in a docker container
Server-Tomcat embedded with the Spring boot.
Database is the existing MS Sql server and it is located OnPrem
Some queries which were earlier took less that 500ms to execute is now taking upto 2000ms. The queries,logic and other code related thigs are same between the legacy and the new application. We are not able to find why the queries are taking more time in this.
In the legacy application we had used the server.xml to configure the data source and in the new application we have configured the data source in the Spring boot application.properties . Below are the Datasource configurations:
Liberty Server datasource :
<dataSource id="Microsoft SQL Server JDBC Driver - DataSource - JVM3" jndiName="jdbc/xxx" containerAuthDataRef="yyy" statementCacheSize="50" isolationLevel="TRANSACTION_READ_COMMITTED">
<jdbcDriver libraryRef="MSSQLJDBCLib"/>
<properties.microsoft.sqlserver databaseName="dbName" serverName="servername.com" portNumber="9999" lockTimeout="2000" packetSize="4096" sendStringParametersAsUnicode="false" trustStorePassword="{xor}" beginTranForVendorAPIs="false" freeResourcesOnClose="false" jmsOnePhaseOptimization="false" reauthentication="false" preTestSQLString="SELECT 1" validateNewConnection="false" validateNewConnectionRetryInterval="3" errorDetectionModel="ExceptionMapping" nonTransactionalDataSource="false" name="Microsoft SQL Server JDBC Driver - DataSource - JVM3" enableMultithreadedAccessDetection="false" beginTranForResultSetScrollingAPIs="false" validateNewConnectionRetryCount="100" connectionSharing="1"/>
<connectionManager agedTimeout="-1" connectionTimeout="180" maxIdleTime="300" maxPoolSize="30" minPoolSize="0" reapTime="240" purgePolicy="FailingConnectionOnly"/>
</dataSource>
Spring Boot Datasource :
spring.datasource.url=jdbc:sqlserver://server.com:9999;databaseName=dBName
spring.datasource.hikari.maximumPoolSize=30
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=180000
What may be the other parameters which can impact the query performance between the old and the new application?
Note : The ping time between the AWS servers and the OnPrem db takes less than 2ms (very negligible).
The issue is fixed. All the queries which we made to the database were doing an Index scan instead of index seek. That is because we missed the sendStringParametersAsUnicode="false" parameter in the Connection JDBC url.
We changed the connection url to take the required parameters and the queries ran perfectly fine.
spring.datasource.url=jdbc:sqlserver://server.com:9999;databaseName=dBName;packetSize=4096;sendStringParametersAsUnicode=false

jee:jndi-lookup use to connect to database

I am working on spring application. I need to connect my application to database to get some records. I am failing to connect to database. It says "Table or view doesn't exist". I have the table and i can run the query in TOAD to view the records.
In dataSource-config.xml, i have the below line.
<jee:jndi-lookup id="cifDataSource" jndi-name="java:jdbc.datasource.CIFDataSource" resource-ref="true" />
What is the use of the above line. Do i need to create any object related to "CIFDataSource" . Please advice.
There appears to be a DataSource configured in your Java EE application server.
You need to find this configuration and point it at the database that contains your tables.

Jboss not re-connects automatically to DB connections on outage

I've this project: https://github.com/AndreaCatania/libreria
I've deployed this project into WildFly 8.2 and when it get outage of the DB connections the AS can not reconnects automatically, and the application is freezed (I have to restart AS).
If I deploy the same project into GlassFish i've no problem.
How can I resolve this issue?
WildFly configuration:
https://github.com/AndreaCatania/libreria/blob/master/standalone-full.xml
https://github.com/AndreaCatania/libreria/blob/master/standalone.xml
On your hibernate.cfg.xml you can add lines like these:
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1</property>
Which will validate connections to Database. Taken from https://confluence.atlassian.com/display/CONF43/Surviving+Database+Connection+Closures
You seem to be using C3P0 which has known problems when the database goes down. You should consider switching to HikariCP which responds better in such situations. Here is an excellent comparison of the various connection pools under database outage.
Alternatively, consider using the pooling functionality provided by the app server.

separating database tasks from spring petclinic app

I want to turn off the spring petclinic's automatic recreation and repopulation of its underlying MySQL database every time the app restarts. Can anyone show me how to do this?
The web.xml for the app can be found at this link. And the other xml config files can be found at this link.
I prefer to run database scripts separately from the application, using the MySQL command line client.
In the file datasource-config.xml locate the following configuration
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${jdbc.initLocation}"/>
<jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>
Just comment out this code, and you should be good to go.

Hibernate configuration for Elastic Beanstalk (AWS)

I am working on a Java application using Hibernate.
I would like to deploy it on Elastic Beanstalk (Amazon Web Services) to be able to scale accordingly.
RDS is the database I want to use.
However, I do not know how to give my configuration settings to Elastic Beanstalk. Apparently it is now possible, without having to create an AMI and to use this AMI for each new server (when autoscaling).
I use :
hibernate.cfg.xml
server.xml (link to hibernate.cfg.xml)
But I want to scale easily, so no "manual configuration of EC2 instance" to input those files.
So how can I give those settings to my application without the two files ?
How to deploy on Elastic Beanstalk with those info ?
Amazon Relational Database Service (RDS) is a web service to setup relational databases in the cloud. RDS supports relational database engines such as MySQL, Oracle, SqlServer. For MySQL change the hibernate.cfg.xml like below
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="connection.url">jdbc:mysql://my_sports_entertainment_db_url/news</property>
<property name="connection.username">my_username</property>
<property name="connection.password">my_password</property>
</session-factory>
Also check How to 'switch' from MySQL to Amazon RDS with minimal application impact?
I can see a way to do this, but it isn't pretty.
Your instance configuration file can include container commands. These run after the container and application are installed, but before the application is started. At this point, it is possible to edit the Tomcat context.xml file to add a datasource. The text you need to add is the usual datasource configuration. Your problem is that you have to do it from a script. The easiest thing might be to write the configuration and deliver it with your application, then use a container command to apply the mighty sed to splice it into the context.xml.
You have another problem in that the actual configuration you need to write must include things like the hostname, username, and password for RDS, which you won't have during development. AWS does expose these to Java through system properties, so the information must be on the machine somewhere. If you could find it, you could mix it into the configuration when you splice it into the context.xml.
Whilst this may be possible, as i said, it isn't pretty. It feels like a hack. There must be a better way of doing this.

Categories

Resources