multiple DBs with Jboss 4.2.3 - java

We are using Jboss 4.2.3 with EJBs and hibernate.
Currently we defined 3 db connections in our persistence.xml file (one for reads (tx-local), one for writes (no-tx) and one for statistics (no-tx)), and when we add a forth db connection which is also (no-tx) and map a couple of entities to it, the machine starts to show an unusual load.
If we remove this connection (no one is using it still) the load get back to normal.
I guess it is some sort of configuration problem with JBoss, but I'm not sure.
any assistance will be appreciated.
thanks.
this is the ds.xml file content:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: feedback-ds.xml 71535 2008-04-01 07:05:03Z adrian#jboss.org $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<no-tx-datasource>
<jndi-name>FeedbackDS</jndi-name>
<connection-url>jdbc:mysql://m6sdb:3306/m6sFeedbacks?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>password</password>
<min-pool-size>10</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>1000</max-pool-size>
<property name="hibernate.show.sql" value="true"></property>
<idle-timeout-minutes>10</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support -->
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</no-tx-datasource>
</datasources>

Related

Configuration of persistence.xml while using websphere 8.5.5.1

I'm coding an app that uses Servlet 3.0, Jsp, and JPA 2.0 and i'm deploying it into Websphere application server 8.5.
Since i already configured into the ibm websphere console, the data source and the jdbc driver, and the j2c authentification (i'm using oracle 11g as a database ). I dont know how my persistence.xml should look like, if i need to specify and add openJPA jars to my project.
For now anything i put into persistence.xml i'm having this issue :
Error 500: <openjpa-2.2.3-SNAPSHOT-r422266:1764177 fatal user error> org.apache.openjpa.persistence.ArgumentException
What should i do ? maybe i'm missing how JPA works
Thanks in advance
The OpenJPA jars should be provided by WebSphere and available to use for your application. There is a JPA sample available here: https://developer.ibm.com/wasdev/downloads/#asset/samples-Java_Persistence_API_JPA_Sample
In the sample, you can see an example of the persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpasamplepersistenceunit">
<jta-data-source>java:comp/env/jdbc/samplejpadatasource</jta-data-source>
<non-jta-data-source>java:comp/env/jdbc/samplejpadatasourcenonjta</non-jta-data-source>
<class>wasdev.sample.jpa.Thing</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- These properties are creating the database on the fly. We are using them to avoid users having
to create a database to run the sample.
See also the create=true line in the datasource meta data. -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="derby" />
<!-- EclipseLink specific properties to create the database. They are only used if using the jpa-2.1 feature. -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
</properties>
</persistence-unit>
</persistence>
I think your error is not caused by the OpenJPA jars not being available. It might be because your database is not configured correctly. Make sure your persistence.xml file refers to your datasources properly.

Create Portable JDBC Connection Pool in Glassfish

I'm trying to deploy my project to AWS Elastic Beanstalk, using Docker with preconfigured Glassfish.
Since the project uses JPA, and EJB to create a transactional persistence unit; I create a database connection pool in the Glassfish admin console, then supply the JNDI string to persistence.xml.
All good in my local machine, Amazon won't allow you to use the admin console. I've read this post, which discusses generally how to configure Glassfish with asadmin, but I've found it very hard to follow and sounds very invasive.
Is there a one-size-fits-all solution to create a database connection pool that is automatically allocated on any server I deploy my application to?
Yes. Although this is unfortunately under-documented.
You can create an application scoped connection pool, that Glassfish creates on the fly, when you depoly your war file to it. It will destroy it when undeployed. Though, it is only available to this application on the server.
Here's what you should do. Create a file named glassfish-resources.xml like this (for MySql):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" name="MySQLPool" res-type="javax.sql.DataSource">
<property name="user" value="someUser"></property>
<property name="password" value="aPassword"></property>
<property name="databaseName" value="aDatabase"></property>
<property name="serverName" value="some.string.you got.from.amazon.rds.amazonaws.com"></property>
<property name="portNumber" value="3306"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="MySQLPool" jndi-name="jdbc/MySQLPool"></jdbc-resource>
</resources>
And drop it into your WEB-INF directory. You should also place the database connection dependency jar in the lib directory.
Now since this isn't a global JNDI reference, as it is only available to this application, you should access it in persistence.xml with java:app prefix, like this:
<jta-data-source>java:app/jdbc/MySQLPool</jta-data-source>

Managed VM not running Cloud SQL Proxy

Launching a Managed VM with a Java application, the following appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>thmadmin-ben</application>
<version>master</version>
<threadsafe>true</threadsafe>
<vm>true</vm>
<manual-scaling>
<instances>1</instances>
</manual-scaling>
</appengine-web-app>
I don't appear to have a Cloud SQL proxy running on the managed VM. Do I need to add an app.yaml file or define something else in appengine-web.xml?
Per the documentation, you need to specify the instances which you will be connecting to as follows and redeploy your application.
When using appengine-web.xml, the syntax would be:
<beta-settings>
<setting name="cloud_sql_instances" value="<PROJECT-ID>:<REGION-NAME>:<SQL-INSTANCE-NAME>[, ...]" />
</beta-settings>
If this setting is not present, the Cloud SQL Proxy won't start.

Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver'

I am facing a very bizarre problem where my App Engine server can't load its Cloud SQL's GoogleDriver, here's the error (It happens right after I run "mvn appengine:update").
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
...
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 48 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.GoogleDriver
appengine.properties
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://mytestapp:testdb?user=someuser
jdbc.username=someuser
jdbc.password=******
jdbc.dialect=org.hibernate.dialect.MySQLDialect
In my Spring context file I have:
<context:property-placeholder location="classpath:appengine.properties" />
<bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property
name="driverClassName"
value="${jdbc.driverClassName}" />
<property
name="url"
value="${jdbc.url}" />
...
Any ideas?
==
Just to confirm, I had already configured my appengine-web.xml and it doesn't help at all:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mytestapp</application>
<version>1</version>
<threadsafe>true</threadsafe>
<sessions-enabled>true</sessions-enabled>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>
--
I couldn't move forward due to some incompatibility between hibernate jar versions so I kept trying through the actual release pipeline (Jenkins + Maven + build + test + deploy), I had to add the Compute Engine VM's IP address to the list of authorized IPs of the Cloud SQL Instance in order to run my unit tests and deploy it to the application (but, if I keep this approach, I can only use the MySQL driver and URL instead of the GoogleDriver and URL. So this is getting tricky...).
I have these properties in a Production.properties file that is loaded by my Spring MVC config file:
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://*******testapp:testsqldb?user=root
jdbc.username=root
jdbc.password=*****
jdbc.dialect=org.hibernate.dialect.MySQLDialect
Is there an easy way to dynamically switch between external and GAE Cloud SQL connection details without using this approach?
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// Connecting from App Engine.
Any ideas?
The class is not automatically available in the App Engine runtime. You must enable it by adding <use-google-connector-j>true</use-google-connector-j> to your appengine-web.xml. This is documented at: https://cloud.google.com/appengine/docs/java/cloud-sql/#enable_connector_j
Edited to add (from comments below):
Also, when connecting to Cloud SQL from GAE you should leave the password field empty.
You should also make sure that if your code is running outside GAE (e.g. on your workstation, on GCE, on a Jenkins build) it uses stock MySQL connector as the Google connector is only available on GAE.
You might also want too look into using the stock MySQL driver, which works both from GAE and other connections. There is a demo of this at https://github.com/GoogleCloudPlatform/appengine-cloudsql-native-mysql-hibernate-jpa-demo-java

Java JNDI lookup failing on JBoss AS 6 from external Java program

I am upgrading from JBoss AS 4.0.4.GA to JBoss 6.0.0.Final.
I have been unable to lookup a HornetQ Topic from an external Java client, so I decided to write a little test Java program that did a simple lookup of a DataSource. That lookup is failing. I can see the DataSource in the web console and it has:
Pool JNDI Name = MyDS
I used to (with JBoss 4.0.4.GA) lookup the DataSource using "java:/MyDS", but that is not working. I have seen some talk about not being able to lookup objects (Queues,Topics,DataSources,EJBs,etc) from outside the AS. I'd be surprised if that is the case, but if it is, then how do I disable that "feature"?
The DS is in server/myconf/deploy/mysql-ds.xml:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xxxxxxxx</user-name>
<password>xxxxxxxx</password>
<prepared-statement-cache-size>50</prepared-statement-cache-size>
<min-pool-size>20</min-pool-size>
<max-pool-size>40</max-pool-size>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
The Java program that looks up the DS is:
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099");
InitialContext ctx = new InitialContext(env);
Object o = ctx.lookup("java:/MyDS");
The lookup line throws a NameNotFoundException:
javax.naming.NameNotFoundException: MyDS not bound
I've tried the lookup with just "MyDS" rather than "java:/MyDS", but it still fails. I don't think the problem is the name I'm using to lookup, rather something preventing me doing any lookup from an external application - and if that is the case, I need to enable such lookups.
Update the datasource definition like:
<use-java-context>false</use-java-context>
e.g.
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xxxxxxxx</user-name>
<password>xxxxxxxx</password>
<prepared-statement-cache-size>50</prepared-statement-cache-size>
<min-pool-size>20</min-pool-size>
<max-pool-size>40</max-pool-size>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

Categories