Glassfish container managed database location - java

I have a glassfish container managed derby database that I can access using CRUD operations. I would like to access my derby database directly through the asadmin tool to view the tables.
However, I cannot find my database.
After researching this site i see that glassfish creates connection pools that connect only when required. I see that it is possible for me to create a connection pool but I don't even know where the database is.
Any advice is greatly appreciated. I am new to JEE7 and learning from 'JEE7 for beginners' book.
<persistence-unit name="chapter15PU" transaction-type="JTA">
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and- create"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>

To connect using derby ij command line tool use the following command:
connect 'jdbc:derby://localhost:1527/sun-appserv-samples';
'sun-appserv-samples' is the default Glassfish container managed db name.
As follows;
Oracle documentation on domain.xml file
If you specify that you're database is to be container managed and you are using Glassfish, then all of the database properties can be found in the domain.xml file.
For example, if you are using the default glassfish domain named domain1, then you must navigate to ..glassfish/domains/domain1 on you're file system. The domain.xml file can be found in the config folder.
Here you will find the following information detailing all of you're database properties.
<property name="PortNumber" value="1527"></property>
<property name="Password" value="APP"></property>
<property name="User" value="APP"></property>
<property name="serverName" value="localhost"></property>
<property name="DatabaseName" value="sun-appserv-samples"></property>
<property name="connectionAttributes" value=";create=true"></property>
As you can see the default database name is sun-appserv-samples.
To connect to this database you must open the command tool ij which can be found in you're derby bin folder. Then use the following command to connect to the database.
connect 'jdbc:derby://localhost:1527/sun-appserv-samples';
Note: You must have derby running and you're java web application deployed on Glassfish first.

I am not sure you can access directly a derby database that is in-memory.

Related

Connecting to Oracle Wallet from Cloud web application

I have recently started deploying my web applications into Google cloud platform. Fortunately, I've solved every annoying errors, exceptions, troubles on my own by researching in stack overflow and other platforms. My current deployed application establish the connection to my real oracle database which is located in Oracle Cloud Infrastructure. While running my web app in localhost, of course it connects because the config file points to wallet folder in my file system. But now in cloud, I don't know where to store my wallet to reference it from my hibernate config xml. And also I don't know if it's possible to reference somewhere other than filesystem, like https://blablafileupload.com/mywalletfolder.
I'm gonna provide my config file below. Can you help me if you know how to do it, and also where is the best place for storing such database wallets (I guess the storage in the location same as my deployment is good place, but I don't know how).
-<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" id="myDataSource">
<property value="oracle.jdbc.driver.OracleDriver" name="driverClass"/>
<property value="jdbc:oracle:thin:#oraclesql_medium?TNS_ADMIN=/Users/user/Desktop/Fuad/Wallet_OracleSQL/" name="jdbcUrl"/>
<property value="TABLE" name="user"/>
<property value="********" name="password"/>
<!-- these are connection pool properties for C3P0 -->
<property value="5" name="minPoolSize"/>
<property value="20" name="maxPoolSize"/>
<property value="30000" name="maxIdleTime"/>
</bean>
As you can see on 3rd line, it refers to the wallet folder in my filesystem ( I want to store that somewhere and make xml refers to it on Internet.)

How create the database through Hibernate

In my spring project, i am using Hibernate to export my entity classes to a previously created database. But this will require the final user knows how to create a database in the Database manager system (Currently I am using Postgresql).
Is there any way of, given only the machine where the postgresql is installed (and the username and password, which is provided when the application is runned the first time), the Hibernate create a new database in the server if it doesn't exist?
If your configuration looks like this
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://host:port/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Then the database will be created by Hibernate automatically.
Update:
Ok now I understand what you want. You want to start the Postgresql server with Hibernate. This is not possible. Hibernate does not do this.
You can do this with
Another script that starts with your application
A maven/ant target.
A build job
But the best solution is to use an in-memory database that does not need an external server (for example H2, or Java derby)
See also
Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
and
Postgres database create if not exists
Take a look of paramater hibernate.hbm2ddl.auto for your hibernate.cfg.xml file. I suggest you this link: Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?
Run "CREATE DATABASE ..." (see http://www.postgresql.org/docs/9.0/static/sql-createdatabase.html) as a native SQL query ...
.createSQLQuery(" ... ").executeUpdate(); ...
Hibernate will - at least as far as I know - not create the database, only the tables in it.
I suppose you need to connect to postgresql via a second persistence unit/connection, because of the chicken-and-egg nature of this approach.

Embedded H2 + Tomcat 7 JDBC realm + Hibernate = "Locked by another process"

Perhaps the title is self explanatory, but I am trying to create a web application with an embedded instance of the H2 database. I am configuring Tomcat 7 to use the JDBC realm to form-based authentication. server.xml has:
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.h2.Driver"
connectionURL="jdbc:h2:/someDir/myDB"
connectionName="userName"
connectionPassword="password"
userTable="user_enabled"
userNameCol="user_name"
userCredCol="pass"
userRoleTable="user_role"
roleNameCol="role_name" />
I am also using Hibernate for persistence. persistence.xml has:
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:/someDir/myDB" />
<property name="hibernate.connection.username" value="userName" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
<property name="current_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext" />
If I try to start the server I get:
(...) Caused by: org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process".
And my web application fails to start.
Starting an H2 TCP server and replacing JDBC URLs with jdbc:h2:tcp://myIP/ works fine. So no problem with my database files. Also, I am sure nothing else is trying to use those files. So the conflict can only be between the JDBC realm and Hibernate.
Why do I want an embedded server? For the classic reasons: I want to distribute this application and I don't want users to have to start two processes instead of just one. Also, this will not be a shared database, so no need to create a new process just for that. Finally, no additional server ports are open for database, which is good for security.
Answering my own question, the way to make this work is by using a JNDI data source to access the database. For authentication, Tomcat should be configured to use the DataSourceRealm. So both Tomcat security and Hibernate will use the same embedded instance of H2, which does not cause the conflict I was experiencing.
Though the JNDI data source is preferable, the original issue is that you are trying to create two connections to the same H2 database using a file or embedded database url. H2 does support this, but both of the processes would need to have ";AUTO_SERVER=true" appended to the connection url. This would cause the first connection to start the db in process in embedded mode, but would allow the seccond connection to connect through tcp ip.
Details are available here:
http://www.h2database.com/html/features.html#auto_mixed_mode

HSQLdb permissions regarding OpenJPA

I'm (still) having loads of issues with HSQLdb & OpenJPA.
Exception in thread "main" <openjpa-1.2.0-r422266:683325 fatal store error> org.apache.openjpa.persistence.RollbackException: user lacks privilege or object not found: OPENJPA_SEQUENCE_TABLE {SELECT SEQUENCE_VALUE FROM PUBLIC.OPENJPA_SEQUENCE_TABLE WHERE ID = ?} [code=-5501, state=42501]
at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
at model_layer.EntityManagerHelper.commit(EntityManagerHelper.java:46)
at HSQLdb_mvn_openJPA_autoTables.App.main(App.java:23)
The HSQLdb is running as a server process, bound to port 9001 at my local machine. The user is SA. It's configured as follows:
<persistence-unit name="HSQLdb_mvn_openJPA_autoTablesPU"
transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>model_layer.Testobjekt</class>
<class>model_layer.AbstractTestobjekt</class>
<properties>
<property name="openjpa.ConnectionUserName" value="SA" />
<property name="openjpa.ConnectionPassword" value=""/>
<property name="openjpa.ConnectionDriverName"
value="org.hsqldb.jdbc.JDBCDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:hsqldb:hsql://localhost:9001/mydb" />
<!--
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
-->
</properties>
</persistence-unit>
I have made a successful connection with my ORM layer. I can create and connect to my EntityManager.
However each time I use
EntityManagerHelper.commit();
It fail with that error, which makes no sense to me. SA is the Standard Admin user I used to create the table. It should be able to persist as this user into hsqldb.
edit: after hours of debugging I found out why this fails. This kind of error message also appears if you do not set required table entries (NOT NULL). It didn't indicate that for me. It seems the OpenJPA layer mistakes not being able to insert statements because of missing entries for permission problems. I simply accepted the first answer therefore. Thanks for reading :)
I have the impressoin that HSQL has no rights to write its datafile in the configured directory.
That happens to me all the time when I test my server manually as root/Administrator and that when starting it as a daemon/service it changes to a less privileged user. Then the files are owned by another user as the server is running as.
It could be other reasons : on Windows I had it when another process (another server instance) was still clinging on to the files, or even when eclipse in its infinite wisdom decided to index the database.

how do I change persistence.xml at run time

I am new to openJPA.
I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml.
For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.
And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.
FYI I am using WS app. server 7 and RAD 7.5
Any type of help would be highly appreciated.
You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.
Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.
Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.
For example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:#localhost:1521:XE
Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.
Of course, you can use JNDI data sources with Spring also.

Categories

Resources