i was trying a basic program in java using hibernate... but im getting the following error description
WARN: HHH000277: Could not bind factory to JNDI
org.hibernate.service.jndi.JndiException: Error parsing JNDI name [hiber]
at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:92)
at org.hibernate.service.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:108)
at org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:89)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:480)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
at manageEmployee.main(manageEmployee.java:26)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
... 6 more
Exception in thread "main" java.lang.ClassCastException: org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction cannot be cast to javax.transaction.Transaction
at manageEmployee.addEmployee(manageEmployee.java:44)
at manageEmployee.main(manageEmployee.java:34)
I fixed this by removing the name attribute from opening session factory tag in my hibernate configuration file so that it looks like the following:
<session-factory>
instead of
<session-factory name="">
Check your imports statement if found this:
import javax.transaction.Transaction;
Then replace this with:
import org.hibernate.Transaction;
And remove casting from all places find where you're casting like below:
tx = (Transaction) session.beginTransaction();
Remove
(Transaction)
to remove casting from all places.
This could be triggert by either there are some depending libarys of hibernate missing or u have imported some false libarys with the same name.
Related
I'm using Flyway 5.2.4 and OSGI Bundle Activator. I want to migrate database on bundle Start() method.
Here's my ActivatorClass:
import org.flywaydb.core.Flyway;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator implements BundleActivator {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private ServiceRegistration serviceRegistration;
public void start(BundleContext bundleContext) {
log.info("Starting...");
serviceRegistration = bundleContext.registerService(IConnector.class.getName(), new Connector(), null);
log.info("Started.");
//FlyWay Section
Flyway flyway = Flyway.configure().dataSource("jdbc:postgresql://localhost:5432/", "postgres", "12345").load();
flyway.migrate();
}
public void stop(BundleContext bundleContext) {
log.info("Stopping...");
serviceRegistration.unregister();
log.info("Stopped.");
}
}
As you can see, I'm using slf4j as my Logger. Maybe that's why I get this errors in my StackTrace after app deploying:
java.lang.Exception: Could not start bundle mvn:internship/db-connector/1.0.0 in feature(s) feature-1.0.0: Activator start error in bundle db-connector [2343].
at org.apache.karaf.features.internal.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:519)[20:org.apache.karaf.features.core:3.0.8]
at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeatures(FeaturesServiceImpl.java:474)[20:org.apache.karaf.features.core:3.0.8]
at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeature(FeaturesServiceImpl.java:415)[20:org.apache.karaf.features.core:3.0.8]
at Proxy683d032e_ad2c_4b9a_98f7_baca7b5564f1.installFeature(Unknown Source)[:]
at Proxyfff1bf4b_671e_4ff6_bb97_e0dabf9f20e8.installFeature(Unknown Source)[:]
at org.apache.karaf.kar.internal.KarServiceImpl.installFeatures(KarServiceImpl.java:282)[89:org.apache.karaf.kar.core:3.0.8]
at org.apache.karaf.kar.internal.KarServiceImpl.install(KarServiceImpl.java:111)[89:org.apache.karaf.kar.core:3.0.8]
at org.apache.karaf.kar.internal.KarServiceImpl.install(KarServiceImpl.java:93)[89:org.apache.karaf.kar.core:3.0.8]
at Proxy40d8d25a_37b2_4855_a381_fbb78daa68ce.install(Unknown Source)[:]
at Proxy068a2f57_120b_4ee8_b953_7c8262bae9a2.install(Unknown Source)[:]
at org.apache.karaf.deployer.kar.KarArtifactInstaller.update(KarArtifactInstaller.java:62)[91:org.apache.karaf.deployer.kar:3.0.8]
at Proxy3c143b4c_a2ef_49d0_870b_21a9b5f74704.update(Unknown Source)[:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.update(DirectoryWatcher.java:1101)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.update(DirectoryWatcher.java:898)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:478)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:355)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:307)[7:org.apache.felix.fileinstall:3.5.2]
Caused by: org.osgi.framework.BundleException: Activator start error in bundle db-connector [2343].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.karaf.features.internal.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:516)[20:org.apache.karaf.features.core:3.0.8]
... 16 more
Caused by: java.lang.ExceptionInInitializerError
at org.flywaydb.core.internal.util.FeatureDetector.isSlf4jAvailable(FeatureDetector.java:96)
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:39)
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)
at org.flywaydb.core.internal.util.FeatureDetector.<clinit>(FeatureDetector.java:25)
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:35)
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)
at org.flywaydb.core.Flyway.<clinit>(Flyway.java:86)
at internship.connectors.postgresConnector.Activator.start(Activator.java:18)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
... 20 more
Caused by: org.flywaydb.core.api.FlywayException: Unable to instantiate class org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator : org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator
at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:63)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:46)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.internal.util.ClassUtils.<clinit>(ClassUtils.java:39)[2348:org.flywaydb.core:5.2.4]
... 30 more
Caused by: java.lang.ClassNotFoundException: org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator
at java.net.URLClassLoader.findClass(Unknown Source)[:1.8.0_212]
at java.lang.ClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at java.lang.ClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at java.lang.Class.forName0(Native Method)[:1.8.0_212]
at java.lang.Class.forName(Unknown Source)[:1.8.0_212]
at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:61)[2348:org.flywaydb.core:5.2.4]
... 33 more
So, I just have no idea what's wrong here, I've used simpliest example from official Flyway's site and get this errors.
I've already try to delete my own Logger. I thought it can let FlyWay to use it's logger, but everything goes the same way (same errors).
Could you help me?
P.S. Looks like this pull request somehow related to my problem.
Indeed it seems Flyway might have issues in OSGi. Maybe you can provide them an issue and your example.
Another issue with your example is that you try to access the DataSource via a url. This does not work in OSGi. The reason is that this way flyway has to have direct access to the database driver classes. This does not work in OSGi.
In OSGi the way to access a Database is with a DataSourceFactory which the database driver creates as a service. From this factory you can create a DataSource.
As not all database drivers offer this service there is pax-jdbc which provides factories for all common databases. It also allows to create a DataSource including pooling from a OSGi config.
Your approach of migrating on bundle start is a very bad idea. The methods in the activator must return quickly and a databse migration might take a while. Of course you want to make sure the migration takes place before any bundle in the system accesses the database. Fortunately there is a way to hook into the DataSource creation to do things like a migration.
See the liquibase tutorial which also shows a database migration. It uses the PreHook offered by pax-jdbc which makes sure your migration code is run before the DataSource is given to any other bundle.
I managed to avoid this error by setting a custom LogFactory before entering flyway's migration code. The basic idea is presented in this sample commit
I am getting the below exception when i run the spring boot application.
But the same code works fine when we execute as a plain java.
Exception:
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.sample.model.TRSN_LOOKUP.emClntId
at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:64)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:230)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4725)
This is for spring-boot standalone application running in windows
Session session = getSession();
if (!session.getTransaction().isActive() )
session.beginTransaction();
TRSN_LOOKUP trsnlookup = new TRSN_LOOKUP();
trsnlookup.setPrsnid(PrsnId);
trsnlookup.setEmClntId(clientId);
session.save(trsnlookup);
session.getTransaction().commit();
Continuation from my above comments:
Just try with naming-strategy property which will help you to use a similar name.
For example,: Spring will change entity name from TRSN_LOOKUP to tRSN_LOOKUP. So, please try adding one more property in hibernate.
There are two types of strategy, you can use 2nd one:
1. org.hibernate.cfg.ImprovedNamingStrategy
2. org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
which help you keep the same name in all DB fields.
So I recently have started working in a new proyect. These guys have the Datasource connection declared this way:
Context init = new InitialContext();
Context context = (Context) init.lookup("java:comp/env");
ds = (DataSource)context.lookup("jdbc/WhateverDS");
I don't know why have they declared it this way. I've had always seen it like this:
DataSource ds = new InitialContext().lookup("java:comp/env/jdbc/WhateverDS");
First of all do you guys know why would they do it in two steps?
My second question is that sometimes we get the following error, and if it may have something to do with the code above:
Caused by: javax.naming.NameNotFoundException: The name comp/env is not bound in this context
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
... 19 more
We are using MySQL over Tomcat using jdbc.
Edit:
They have nothing about the DB declared in web.xml.
The context.xml (among other parameters) looks like this:
Resource driverClassName="com.mysql.jdbc.Driver"
name="jdbc/WhateverDS"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/someDB"
To answer your first question: there is no effective difference between the two-step and the one-step lookup for the DataSource.
Your second question appears to be a duplicate of javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context
According to this documentation:
https://www.playframework.com/documentation/2.5.x/JavaDatabase#exposing-the-datasource-through-jndi
I simply need another entry in my application.conf to expose a DataSource in JNDI:
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.jndiName=DefaultDS
I've added "tyrex" % "tyrex" % "1.0.1" to my libraryDepenencies in build.sbt.
From reading several other posts on this, it sound like I should be able to simply use
DataSource ds = (DataSource) play.api.libs.JNDI.initialContext().lookup("DefaultDS");
To fetch the DataSource from JNDI. However, when I try this it throws the following Exception:
javax.naming.NameNotFoundException: DefaultDS not found
at tyrex.naming.MemoryContext.internalLookup(Unknown Source)
at tyrex.naming.MemoryContext.lookup(Unknown Source)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
The main reason I'm trying to do this is so that Quartz can re-use the DataSource/ConnectionPool created by Play instead of defining another in quartz.properties. According to the docs:
http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigDataSources.html
I should need the following two lines in quartz.properties:
org.quartz.jobStore.dataSource = h2
org.quartz.dataSource.h2.jndiURL = DefaultDS
However, Quartz throws a bunch of exceptions:
java.sql.SQLException: Could not retrieve datasource via JNDI url 'DefaultDS' javax.naming.NameNotFoundException: DefaultDS not found [See nested exception: java.sql.SQLException: Could not retrieve datasource via JNDI url 'DefaultDS' javax.naming.NameNotFoundException: DefaultDS not found]
I'm not sure where to go next. Any help would be appreciated.
Thanks.
Found it.
I simply needed to add jdbc to my libraryDependencies in build.sbt. This created and exposed the DataSource in JNDI.
I am using webflow for the first time and I am trying to set up JSR-303 Bean Validation. According to the documentation:
http://static.springsource.org/spring-webflow/docs/2.3.x/reference/htmlsingle/spring-webflow-reference.html#view-validate
I need to add a validator to my flow-builder-services.
I do this and I get the following error in my source:
cvc-complex-type.3.2.2: Attribute 'validator' is not allowed to appear in element 'webflow:flow-builder-services'.
When I run the app I get the following error:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [webflowContext.xml]
Offending resource: class path resource [spring-servlet.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 18 in XML document from class path resource [webflowContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'validator' is not allowed to appear in element 'webflow:flow-builder-services'.
It seems the flow config is set up correctly I can navigate from my welcome page to my second page in my flow just fine.
I am using Spring-3.0.5, Spring-Webflow-2.3.0
Any help would be much appreciated.
Thanks
Found the answer to this myself.
Make sure to use
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
in the declaration statement.
The manual and the ebooks I was using showed
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
Hope that helps someone.