JPA entity manager factory creation failing; JBoss, Hibernate and SQL Server - sql-server

Running JBoss 5.1, with Hibernate as the JPA provider. Backed with SQL Server 2008.
I'm receiving an error at server startup, which is java.lang.ClassCastException: org.hibernate.dialect.SQLServerDialect cannot be cast to org.hibernate.dialect.Dialect. Pretty clear message, but I'm baffled as to the underlying cause. I have hibernate-core-3.5.1-Final.jar on the classpath, and the necessary class files are present.
This dialect setting was giving no error in the project when it was being used as a property being passed to a Spring AnnotationSessionFactoryBean, but I'm trying to refactor a piece to straight EJB/JPA. For what it's worth, here's my persistence config:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="[my name here]">
<jta-data-source>java:jdbc/[my name here]</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
</properties>
</persistence-unit>
I hate these kind of "here's my config and my stacktrace" questions, but I've been poking around on this for an hour and a half and am failing to come up with any new ideas.

The ClassCastException is caused by having two copies of the javax.persistence APIs in your system. When running on JBoss, you are just not supposed to package persistence jar in your application. Remove all the persistence related jar's from your application and the exception should go away.

Related

Config file Ignite discovery in zeppelin, config xml not working even after IgniteConfiguration bean is present

I am using zookeeper based discovery for ignite cluster and ignite nodes are getting properly connected.
When i am setting same config.xml file in zeppelin ignite interpreter in , I am not able to run the ignite. it fails with below exception.
It gives me below error on zepplin:
Failed to find configuration in: file:////tmp/shared.xml
Failed to find configuration in: file:////tmp/shared.xml
Exception details: please click this link
ignite_client.xml i am using:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="clientMode" value="true"/>
<property name="peerClassLoadingEnabled" value="false"/>
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder">
<property name="zkConnectionString" value="zookeeper:2181"/>
</bean>
</property>
</bean>
</property>
<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="testcache"/>
<property name="startSize" value="10"/>
</bean>
</list>
</property>
</bean>
I saw the Ignite code and found that its failing because spring bean is not found of type IgniteConfiguration, but in xml it is actually present as can be seen in shared xml above.
Analysis so far
Debugged zeppelin and Ignite code and verified correct xml file is passed to ignite.start
Verified even content of file tobe double sure by reading inputStream and file content is correct , inputStream is from org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.support.EncodedResource)
On further debug verified that beanDefinitionMap in ApplicationContext contains IgniteConfiguration, attaching snapshot
Tried getting bean by name , it works ... instead of cfgMap = springCtx.getBeansOfType(cls); in org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl#loadConfigurations(java.net.URL, java.lang.Class, java.lang.String...)
For sake of trial I used sprintContext.getBean(), assuming it would run, but its getting weired , I got below class cast exception
After analysis so far, only thing I can understand is some how IgniteConfiguration loaded by diff classloader is causing issue, but dont know why its happening
Debugged further and confirmed, it is because IgniteConfiguration loaded by different class loader, one loaded already was from sun.misc.Launcher.AppClassLoader#AppClassLoader and in sprinContext IgnitConfiguration bean present is loded by scala.reflect.internal.util.ScalaClassLoader and that is why the error "Failed to find configuration in: file:////tmp/shared.xml"
Logically I can think it as spark program is scala code and spring bean loaded at runtime that is why this mismatch
Is this normal ?
I tried your configuration and it works for me. Did you have another version of the file before? If so, I think it was not updated properly, because the only way to get this exception is having valid Spring XML file, but without IgniteConfiguration bean.
Are you sure you actually use the correct file? Can this be caused by the fact that you have it in /tmp folder?

JPA2: can an EntityManager not created via EJB injection still use JTA transactions

I have the following:
An JAR module, containing a DAO class like so:
public class WhateverDao {
#PersistenceContext(name="pu")
EntityManager entityManager;
public Whatever saveOrUpdate(Whatever entity) {
//entityManager.joinTransaction();
return entityManager.merge(entity);
}
The jar also contains a persistence.xml like so:
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/jee6Test_mysql_datasource</jta-data-source>
<!-- Entities classes declared here -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
</properties>
</persistence-unit>
</persistence>
and an empty beans.xml file.
This jar is used inside a web application (a WAR file), where the DAO is injected into a stateless EJB like so:
#Stateless
#LocalBean
public class WhateverEJBImpl {
#Inject
private WhateverDao whateverDao;
public void saveWhatever(Whatever whatever) throws IOException {
whateverDao.saveOrUpdate(whatever);
}
My issue basicaly is that I can't manage to make this code actually persist something in the database. As far as I understand:
my EntityManager should be "Container Managed" (due to how I configured it in the persistence.xml, and the fact that I obtain it via #PersistenceContext annotation, finally also because the jar is part of a WAR file which is deployed in JBOSS EAP 6.2 - also tried with WebLogic 12c).
The fact that I'm calling it through a stateless EJB means that, by default, there's already a transaction available, so there's no need for me to manage it manually
However, using the above described example simply doesn't persist any data to the db (when calling the EJB saveWhatever method).
What I've also tried tried:
calling entityManager.joinTransaction() before calling entityManager.merge(...) (commented line in the DAO code snippet). This gives the error:
javax.ejb.EJBException: javax.persistence.TransactionRequiredException: No active JTA transaction on joinTransaction call
manually calling entityManager.getTransaction().begin() and commit() (before and after entityManager.merge(...). This gives the error:
javax.ejb.EJBException: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
So, my question is, please: How do I:
make it so I can have a separate DAO class, in a separate JAR, that uses EntityManager without programmatically (manually) dealing with transactions
Integrate this DAO-classes-containing jar into a WebApplication that uses EJB so that I can take advantage of JTA and have it auto-manage my transactions (more to the point, I'd like to have the Stateless EJB promised behavior, where JTA takes care of creating a transaction for each of the EJB's method calls)
OK, well, for anyone else experiencing this: if you think everything's correctly setup with your EJBs and JTA, but still JTA transactions don't work, (fail silently, etc), here's how I solved it:
I changed from this persistence unit:
<!-- bad persistence.xml, JTA transactions don't work -->
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/jee6Test_mysql_datasource</jta-data-source>
<!-- <class> tags go here -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
</properties>
</persistence-unit>
To this one:
<!-- good persistence.xml, JTA transactions work -->
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- note change here ->
<jta-data-source>jdbc/jee6Test_mysql_datasource</jta-data-source>
<!-- <class> tags go here -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- and removal of <property> here -->
</properties>
</persistence-unit>

Why does Persistence.createEntityManagerFactory("transactions-optional") errors when appengine is deployed

I have created an appengine which runs fine and returns records from my Google cloud SQL db in debug mode. When I deploy the appegine and run the endpoint it errors. And the error I get in the appengine log is:
Caused by: org.datanucleus.exceptions.NucleusUserException: There is no available StoreManager of type "jdbc". Make sure that you have put the relevant DataNucleus store plugin in your CLASSPATH and if defining a connection via JNDI or DataSource you also need to provide persistence property "datanucleus.storeManagerType".
Here is a list or the jars in war\WEB-INF\lib directory:
appengine-api-1.0-sdk-1.7.6
appengine-api-labs
appengine-endpoints
appengine-jsr107cache-1.7.6
asm-4.0
com.sun.tools.xjc_2.2.0
com.sun.xml.bind_2.2.0.v201004141950
datanucleus-api-jdo-3.1.3
datanucleus-api-jpa-3.1.3
datanucleus-appengine-2.1.2
datanucleus-core-3.1.3
eclipselink
eclipselink-jpa-modelgen_2.4.1.v20121003-ad44345
gcm-server
geronimo-jpa_2.0_spec-1.0
javax.activation_1.1.0.v201108011116
javax.mail_1.4.0.v201005080615
javax.persistence_2.0.4.v201112161009
javax.xml.bind_2.2.0.v201105210648
javax.xml.stream_1.0.1.v201004272200
jdo-api-3.0.1
json_simple-1.1
jsr107cache-1.1
jta-1.1
org.eclipse.persistence.jpars_2.4.1.v20121003-ad44345
Here is my persistence.xml:
<persistence 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" version="2.0">
<persistence-unit name="transactions-optional" transaction-type="RESOURCE_LOCAL">
<provider></provider>
<class>com.example.myapp.Class1</class>
<class>com.example.myapp.Class2</class>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.google.appengine.api.rdbms.AppEngineDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms://something.com:someproj:someapp/somedb"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
Using:
cloud sql
JPA
Datanucleous v2
SDK 1.7.6
Jre7
As it works fine in debug mode I dont understand what the problem might be as I am new to appengine. Please let me know if you need more information.
Thanks guys.
Make your mind up which JPA provider you're using ... for Cloud SQL (not AppEngine, so you don't need "datanucleus-appengine", nor do you need "datanucleus-api-jdo" either FWIW). You have DataNucleus JPA (version 3 actually, but omitting datanucleus-rdbms jar for some reason, yet CloudSQL needs it), and you have EclipseLink also. You then don't set the persistence provider in persistence.xml so leave it all to chance which one it tries to give you ... and it tries DataNucleus JPA but you haven't included the datanucleus-rdbms jar so you can't persist to an RDBMS without that.

SQLException: Illegal connection port value ${env.OPENSHIFT_MYSQL_DB_PORT}

I am trying to deploy a fairly basic Spring+Hibernate web app to my newly created app. (I am new to openshift).
I have followed this example from the knowledge base.
But after deploying the application i am getting
..
java.sql.SQLException: Illegal connection port value '${env.OPENSHIFT_MYSQL_DB_PORT}'
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2018)
..
I have defined the datasource in my applicationcontext.xml
This is the section in my applicationContext.xml that I define the deta shource (eli is the database name)
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/eli" p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
Any idea what I am doing wrong?
Thanks
The error you're receiving seems to indicate that the property replacement strings in your Spring XML configuration aren't actually being processed by Spring. You can add a PropertyPlaceholderConfigurer to your configuration to enable processing from the system environment:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="searchSystemEnvironment" value="true" />
...
</bean>

Cloud Foundry + JPA + Spring + EntityManager

I want to deploy a Web based WAR into CloudFoundry using a cloud datasource.
I have defined a bean like this:
<cloud:data-source id="dataSource"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="myPersistenceUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
</bean>
</beans>
....but when i try to deploy I obtain a Java Agent exception :
Cannot apply class transformer without LoadTimeWeaver specified
But i can't specfy a Java Agent whithout uploading a jar instrument agent.....
Any solution??
I'm not that familiar with EclipseLink, so I'm not even sure that the weaver requirement comes from EclipseLink, BUT
One way of doing this would be to deploy your app as a standalone app (i.e. deploy your own tomcat around it), that way you can put the extra weaver jar in lib/.
Have a look at http://blog.cloudfoundry.org/2012/06/18/deploying-tomcat-7-using-the-standalone-framework/ and https://github.com/ericbottard/cloudfoundry-tomcat-7
Also, if you only have one DataSource, you can avoid using the cloudfoundry-specific <cloud:datasource /> namespace by just uploading your regular app and have Cloud Foundry auto reconfigure your app (info here: http://blog.springsource.org/2011/11/04/using-cloud-foundry-services-with-spring-part-2-auto-reconfiguration/) As a matter of fact, this may be the way to have EclipseLink work seamlessly on Cloud Foundry (my guess here)
And of course, there's also the option of switching to another provider that doesn't require class instrumentation, but that's a bit extreme.
Again, I'm not an Eclipse Link guru, so I may be missing something here...

Resources