Get GlassFish jdbc pool settings from java code programmatically - java

I create JDBC Connection pool for my Oracle db inside GlassFish admin console.
This pool has some properties, including URL=jdbc:oracle:thin:#//192.168.1.250:1521/MYDB
I'd like to read this jdbc property from my java code (I use web project + ejb, simplified packaging inside .war file).
How can I do that?

You can find all the information you are looking for in the domain's XML file, usualy located at: %glassfish_home_directory%\glassfish\domains\%NAME_OF_YOUR_DOMAIN%\config\domain.xml
you can easily parse it using java's built in support for XML parsing, such as DOM

You can also try using the embedded API to run the asadmin command to get results. Check out this blog entry using the CommandRunner object:
http://weblogs.java.net/blog/bhavanishankar/archive/2011/09/12/embedded-glassfish-programatically-running-asadmin-commands-servlet
perhaps you can try "list-jdbc-resources" or similar asadmin command to get the data.
Hope this helps.

Related

How to see all tables in my h2 database at localhost:8082?

I use JDBC and created h2 database called usaDB from sql script. Then I filled all tables with jdbc.
The problem is that after I connect to usaDB at localhost:8082 I cannot see on the left tree
my tables. There is only INFORMATION_SCHEMA database and rootUser which I specified creating usaDB.
How to view the content of tables in my h2 database?
I tried query SELECT * FROM INFORMATION_SCHEMA.TABLES.
But it returned many table names except those I created. My snapshot:
I had the same issue and the answer seems to be really stupid: when you type your database name you shouldn't add ".h2.db" suffix, for example, if you have db file "D:\somebase.h2.db" your connection string should be like "jdbc:h2:file:/D:/somebase". In other way jdbc creates new empty database file named "somebase.h2.db.h2.db" and you see what you see: only system tables.
You can use the SHOW command:
Using this command, you can lists the schemas, tables, or the columns of a table. e.g.:
SHOW TABLES
This problem drove me around the twist and besides this page I read many (many!) others until I solved it.
My Use Case was to see how a SpringBatch project created in STS using :: Spring Boot :: (v1.3.1.RELEASE) was going to behave with the H2 database; to do the latter, I needed to be able to get the H2 console running as well to query the DB results of the batch run.
This is what I did and found out:
Created an Web project in STS using Spring Boot:
Added the following to the pom.xml of the latter:
Added a Spring configuration file as follows to the project:
This solves the Web project deficiencies in STS. If you run the project now, you can access the H2 console as follows: http://localhost:8080/console
Now create a SpringBatch project in STS as follows (the alternative method creates a different template missing most of the classes for persisting data. This method creates 2 projects: one Complete, and the other an initial. Use the Complete in the following.):
The SpringBatch project created with STS uses an in memory H2 database that it CLOSES once the application run ends; once you run it, you can see this in the logging output.
So what we need is to create a new DataSource that overrides the default that ships with the project (if you are interested, just have a look at the log messages and you will see that it uses a default datasource...this is created from:
o.s.j.d.e.EmbeddedDatabaseFactory with the following parameters:
Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa')
So, it starts an in memory, and then closes it. You have no chance of seeing the data with the H2 console; it has come and gone.
So, create a DataSource as follows:
You can of course use a properties file to map the parameters, and profiles for different DataSource instances...but I digress.
Now, make sure you set the bit that the red arrow in the picture is pointing to, to a location on your computer where a file can be persisted.
Running the SpringBatch (Complete project) you should now have a db file in that location after it runs (persisting Person data)
Run the Web project you configured previously in these steps, and you WILL :=) see your data, and all the Batch job and step run data (et voila!):
Painful but rewarding. Hope it helps you to really BOOTSTRAP :=)
I have met exactly this problem.
From what you describe, I suppose that you connect your jdbc with the "real" h2 server, but you are connecting on web application to database by the wrong mode (embedded-in-memory mode, aka h2mem). It means that h2 will create a new database in-memory, instead of using your true database stored elsewhere.
Please make sure that when you connect to this database, you use the mode Generic H2 (Server), NOTGeneric H2 (Embedded). You can refer to the picture below.
Version of jar file and installed h2 database should be same.
If in case you have created and populated H2 database table using maven dependency in spring boot, then please do change the JDBC URL as jdbc:h2:mem:testdb while connecting to H2 using web console.
It is an old question, but I came across the same problem. Eventually I found out that the default JDBC URL is pointing a test server rather than my application. After correcting it, I could access the right DB.
I tried with both Generic H2 (Embedded) and the Generic H2 (Server) options, both worked as long as the JDBC URL: is provided correctly.
In grails 4.0.1 the jdbc URL for development is jdbc:h2:mem:devDb. Check your application.yml file for the exact URL.
For the people who are using H2 in embedded(persistent mode) and want to "connect" to it from IntelliJ(other IDEs probably apply too).
Using for example jdbc url as follows: jdbc:h2:./database.h2
Note, that H2 does not allow implicit relative paths, and requires adding explicit ./
Relative paths are relative to current workdir
When you run your application, your workdir is most likely set to your project's root dir
On the other hand, IDE's workdir is most likely not your project's root
Hence, in IDE when "connecting" to your database you need to use absolute path like: jdbc:h2:/Users/me/projects/MyAwesomeProject/database.h2
For some reason IntelliJ by default also adds ;MV_STORE=false. It disables MVStore engine which in fact is currently used by default in H2.
So make sure that both your application and your IDE use the same store engine, as MVStore and PageStore have different file layouts.
Note that you cannot "connect" to your database if your application is using it because of locking. The other way around applies too.
In my case the issue was caused by the fact that I didn't set the h2 username, password in java. Unfortunatelly, Spring didn't display any errors to me, so it was not easy to figure out. Adding this lines to dataSource method helped me fix the issue:
dataSource.setUsername("sa");
dataSource.setPassword("");
Also, I should have specified the schema when creating tables in schema.sql
Selecting Generic H2 (Server) solved for me. We tempted to use default Generic H2 (Embedded) which is wrong.

Howto setup HSQLDB schema for Quartz scheduler

I'm writing a "simple" scheduler application which uses the great Quartz Scheduling framework.
Quartz works with most database systems (MySQL, Oracle, HSQLDB, ...), which just need to be specified in the quartz.properties config file. The database used must be initialized with the corresponding SQL init-script, which may be found in the Quartz installation's docs/dbTables directory.
Using a MySQL database, I just had to run the tables_mysql.sql script on the database and everything was fine.
Now I intend to switch to an in-memory DB (HSQLDB) running in file mode. Starting my app seems to create the HSQLDB correctly, but now I don't find an easy way to run the init-script on this database.
Checking Google and Stackoverflow I found some solutions, but they require either spring-framework, hibernate, Flyway or programmatically getting a connection and parsing/executing the statements in the init script.
My question: isn't there an easier way to set up an HSQLDB for Quartz? Ideally Quartz would do it by itself, since it knows the DB connection data from its config file as well as the required setup script... Am I missing something?
Thanks for any hints on this!
You can use SqlFile which is part of SqlTool. This is a separate jar, sqltool.jar, in the HSQLDB zip package.
This can be used with HSQLDB as well as other database engines.
The usage is covered in its own guide:
http://hsqldb.org/doc/2.0/util-guide/sqltool-chapt.html
Easy way is to get the schema file for your quartz version(tables_hsqldb.sql) and give the path of the schema file in application.properties file and then spring will create the schema on its own.Else you can place the schema file in resources folder. Then you need to inform the datasource to quartz so that quartz will take care of everything.

How to index data in single solr app using tomcat server

I am working on single solr app. I downloded solr exampple code for net, which is working fine while running on jetty server.It is having data which are to be indexed in C:\apache-solr-1.4.0\example\exampledocs and the indexes are stored in C:\apache-solr-1.4.0\example\solr\data, using jetty server indexes are created using command java -jar post.jar *.xml. Now i want to know how can i achieve this using Tomcat. do i need to change the configuration to change the path for indexe storage and for xml files storage. how data will b indexed so that i would able to search it.
If I understand your question correctly, you'll want to use the -Durl flag when running post.jar, e.g.:
java -jar -Durl=http://localhost:8080/solr/update post.jar solr.xml monitor.xml
In solrconfig.xml you can mention the path that has to hold the index
<dataDir>${solr.data.dir:}</dataDir>
I think you just have to read more from SOLR documentation, and click through what you have in the package.
There is an tomcat deployment doc in solr wiki:
http://wiki.apache.org/solr/SolrTomcat
And the war file is in the dist folder you've downloaded.
How to search it? There is no simple answer. I suggest you read more on the solr wiki. Find out what is a handler, what is the difference between dismax handler and standard handler, how schema.xml defines the database.

Creating a database changelog xml file from an existing database (including stored procs) using Liquibase

Is it possible to create an initial database changelog xml file from the existing state of the database?
I believe I've generated the schema using generateChangeLog, but it doesn't seem to return the stored procedures (or the data).
I'm using SQL Sever 2008
You can return the data using a diffTypes flag that includes "DATA". See http://www.liquibase.org/documentation/diff.html.
Liquibase cannot currently output stored procedures, however. For that you will need to use a different tool and include them in the generated changelog using the tag.
download liquibase.jar , database driver and save to one directory (Ex:/home/mySystem/liquibase), in the command line change the directory to (/home/mySystem/liquibase) and run the below mentioned command
java -jar liquibase.jar --driver=org.postgresql.Driver --classpath=postgresql-42.1.3.jar --changeLogFile=db.changelog.xml --url="jdbc:postgresql://localhost:5432/<databasename>" --username=<username> --password=<password> update
Reference link:
http://www.liquibase.org/documentation/generating_changelogs.html
I just yesterday discovered SQL Power Architect, which seems to be able to generate Liquibase configurations:
http://www.sqlpower.ca/page/architect
For more info on this combination see this blog post:
http://blog.mgm-tp.com/2010/11/data-modeling-part2/

Programaticly create datasource for JBoss 4.2.x

Would it be possible to programmaticly create a data source in jboss and still have a valid jndi entry for the entity manager to use?
Creating the data source is where I am lost, I hope I can use a MBean that runs on stat-up to handle this.
This would not be my preferred method, but the application I am working on has a global configuration file hosted on another server I am suppose to use for configuration.
update: In this instance I need to create a data source programticly or change the jdbc url of an exsiting datasource. I don't know the DB server url until runtime.
Rather than poking around in the guts of JBoss in order to do this, I suggest using a 3rd-party connection pool utility, such as Apache Commons DBCP. There are instructions on how to programmatically register a DBCP datasource on JNDI here.
The first two lines of the sample code should be unnecessary, just create the default InitialContext and then rebind the datasource reference into it as described.
Here's a post that describes how to create a jboss service archive (SAR) that you can put in your EAR that will deploy a data source when the EAR is deployed, and remove it when the EAR in undeployed.

Categories