Hibernate Session's get() function retrieves question marks instead of greek characters - spring

I have a web application that uses spring and hibernate. My hibernate session factory is configured in spring as:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2005Dialect</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.example.dslibweb.model</value>
</list>
</property>
</bean>
The data source as:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxConnections}" />
</bean>
and the properties file for the data source is:
jdbc.username=sa
jdbc.password=***
jdbc.url=jdbc:sqlserver://10.62.0.105:1433;databaseName=example;useUnicode=true;characterEncoding=utf-8
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.maxConnections=-1
I have a call:
DsActions action = (DsActions) this.hibernateCriteriaCommons.findById(id, new DsActions());
and findById is defined as:
public T findById(String id, Object model) {
return (T) this.sessionFactory.getCurrentSession().get(model.getClass(), id);
}
So I am calling the get method of hibernate session for a specific id and I expect an instance of type DsActions.
All works well when I run it from a local Tomcat instance (run through netbeans).
When I install it on a remote tomcat server, the instance of DsActions seems to have an encoding issue. When retriveing a field of DsActions instance I get question marks (??? ?????? ??????). The text is supposed to be greek characters
I am very confused, I do not understand why in the first case it is working and not in the second.
Note: the data is retrieved by the same database server, so no difference there. The only difference is the machine where the application is running.
Thank you all in advance.

I'm pretty sure that Hibernate retrieves that field correctly, and the problem is in the way you output these characters.
As a quick check you can add a condition such as f.contains("?") to your code and output its result - it should be false (if original string doesn't contain ?s, of course).
For possible problems in output see Unicode - How to get the characters right?.

Related

Spring and H2 or Derby - Multiple Transactions

I have a project that uses Spring. In local environment I use Derby (with Jetty) that gives me the perfomance that I need in this environemnt (in production is used Oracle and Weblogic).
The problem is that I need new transactions in some specific operations. This specific operations use the annotation #Transactional(propagation=REQUIRES_NEW).
The problem is that with Derby when comes the time that a new transaction was supposed to be createad, the operation hangs and due time out. I tried to use H2 but in the same moment of creation of the new transaction, the database is restarted, meaning, the tables are dropped and are created again.
Derby and H2 don't support multiple transactions? I can't find anything that tells me the opposite.
My database configuration is:
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
<property name="persistenceUnitName" value="ORACLE_PU"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="shared-cache-mode">NONE</prop>
<prop key="eclipselink.cache.shared.default">false</prop>
<prop key="eclipselink.query-results-cache">false</prop>
<prop key="eclipselink.weaving">false</prop>
<prop key="eclipselink.ddl-generation">${oracle.eclipselink.ddl.generation}</prop>
<prop key="eclipselink.ddl-generation.output-mode">database</prop>
<prop key="eclipselink.create-ddl-jdbc-file-name">create-tables.sql</prop>
<prop key="eclipselink.drop-ddl-jdbc-file-name">drop-tables.sql</prop>
<prop key="eclipselink.application-location">target/generated-sources/database</prop>
<prop key="eclipselink.jdbc.cache-statements">true</prop>
<prop key="eclipselink.custom.sql.import.file">${oracle.eclipselink.custom.sql.import.file}</prop>
...
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:target/memory;create=true"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
My attempt with H2 is similar but with the proper configurations. In weblogic environemnt everything works as expected.
What I'm missing?
It seems you are using derby in embedded in-memory mode. AFAIK, both derby and H2 support concurrent transactions only in standalone mode, when you connect through a TCP connection, but not in embedded mode. Try running derby as a seperate procese and change your database to connect to it.

autocreate schema with jdo, spring and H2 with datanucleus

I Recently migrated to using the spring framework for DI - working fine. I'm injecting a persistence manager which also works fine. On a new install, I get:
SEVERE: Required table missing .... Either your MetaData is incorrect, or you need to enable "datanucleus.autoCreateTables"
Fair enough, I'm not enabling autocreate tables.
I create my persistence manager like this in the spring context.xml per the doc:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:./thedbpath.db;MV_STORE=FALSE;MVCC=FALSE;FILE_LOCK=NO"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="pmf" class="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" destroy-method="close">
<property name="connectionFactory" ref="dataSource"/>
<property name="nontransactionalRead" value="true"/>
</bean>
everything works - but i can't figure out where to set the datanucleus.autoCreateTables
This normally would be set in the persistence.xml - I don't see where to put datanucleus properties in the spring context.xml. Thanks in advance
edit: thanks to the answer below, this was the correct config:
<bean id="pmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
<property name="jdoProperties">
<props>
<prop key="javax.jdo.PersistenceManagerFactoryClass">
org.datanucleus.api.jdo.JDOPersistenceManagerFactory
</prop>
<prop key="javax.jdo.option.ConnectionURL">jdbc:h2:./database/db;MV_STORE=FALSE;MVCC=FALSE;;FILE_LOCK=NO</prop>
<prop key="javax.jdo.option.ConnectionUserName">sa</prop>
<prop key="javax.jdo.option.ConnectionPassword"></prop>
<prop key="javax.jdo.option.ConnectionDriverName">org.h2.Driver</prop>
<prop key="org.jpox.autoCreateSchema">true</prop>
<prop key="org.jpox.identifier.case">PreserveCase</prop>
<prop key="datanucleus.autoCreateTables">true</prop>
</props>
</property>
</bean>
This page
http://www.datanucleus.org/products/accessplatform_3_0/guides/jdo/springframework/index.html
has a "jdoProperties" property that can be used to specify JDO implementation-specific properties. Maybe try that?

Hibernate temporary datastore

I doing unit testing on spring-hibernate DAOs... configured using
#ContextConfiguration(
locations = {
"classpath:test-applicationContext.xml"
})
but it looks like its transacting with the actual database.
How do I use a temporary datastore with out working on actual database
Define your data store on a separate file and include that xml file with your mail application xml. When testing, include a separate xml file to hold your data store pointing to another database such as hsql. Then this will be the data source referred to by your main applicationContext.xml.
Thanks Guys I used H2 and got it working:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url"
value="jdbc:h2:mem:processdb;INIT=RUNSCRIPT FROM 'classpath:create.sql'" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stateDAO" class="com.tutorial.jquery.dao.impl.StateDAOImpl"></bean>
<bean id="stateService" class="com.tutorial.jquery.service.impl.StateServiceImpl"></bean>

Spring,Quartz and JobStoreTX

By using CronTriggerBean i have created two triggers(say trigger A & B) for invoking two different action. We have placed our project as .war in 2 servers.I am using JobStoreTX for clustering. The problem i am facing is that i want to cluster only one trigger i.e A and trigger B should not be clustered. Is there a way to specify through code which trigger has to be clustered. My code goes on as below.
<bean id="shedulerJobStoreTX" class="org.quartz.impl.jdbcjobstore.JobStoreTX>
<property name="driverDelegateClass" value="org.quartz.impl. jdbcjobstore.oracle.OracleDelegate">
<property name="dataSource" value ="jndiDS"/>
<property name="tablePrefix" value="someschemaName"/>
<property name="isClustered" value="true"/>
<property name="clusterCheckinInterval" value="${someValue}"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<ref bean="clusterCronTrigger"/>
</list>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runAppJob" />
<property name="cronExpression" value="${someTimeValue1}"/>
</bean>
<bean id="clusterCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runClusterJob" />
<property name="cronExpression" value="${someTimeValue2}"/>
</bean>
Hi, I have refined the code and the above said issue is not happening.PFB the code for the same.
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<ref bean="clusterCronTrigger"/>
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.jobStore.isClustered">false</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key ="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.
JobStoreTX </prop>
<prop key="org.quartz.jobStore.tablePrefix">schemaName</prop>
<prop key="org.quartz.jobStore.driverDelegateClass"> org.quartz.impl.jdbcjobstore.oracle.OracleDelegate</prop>
<prop key="org.quartz.jobStore.selectWithLockSQL"> SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME= ?</prop>
</props>
</property>
<property name="dataSource" ref="fsmJndiDataSource"/>
But the problem on executing the above code is that i am getting the below exception when the quartz is trying to get the lock ..."Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: ORA-00942: table or view does not exist"
you need to create required quartz tables in database before you run scheduler.
default quartz tables sql script is available at \docs\dbTables..
example if you are using quartz 2.2.1 distribution (quartz-2.2.1-distribution.tar\quartz-2.2.1\docs\dbTables\tables_oracle)
execute the sql script on database and make sure you see the tables created in it refer below image
Due to the message "ORA-00942: table or view does not exist", I think the table "schemaNameLOCKS" is missing. Indeed, from the doc:
org.quartz.jobStore.selectWithLockSQL
Must be a SQL string that selects a row in the "LOCKS" table and
places a lock on the row. If not set, the default is "SELECT * FROM
{0}LOCKS WHERE SCHED_NAME = {1} AND LOCK_NAME = ? FOR UPDATE", which
works for most databases. The "{0}" is replaced during run-time with
the TABLE_PREFIX that you configured above. The "{1}" is replaced with
the scheduler's name.

Spring PropertyPlaceholderConfigurer Default Properties Not Read

I am trying to embed activemq broker in a Tomcat. The code base will be deployed in different environments. I want to externalize some parameters, but want to provide default values for those parameters in case the deployed environment does not provide values for place holders.
This is what I have :
<property name="properties">
<props>
<prop key="embed.broker.networkConnectorURI">static:(failover:(tcp://server01:61616,tcp://server02:61616))
</prop>
<prop key="embed.broker.transportConnectorURI">vm://localhost:61616</prop>
</props>
</property>
<bean id="broker" class="org.apache.activemq.broker.BrokerService"
init-method="start" destroy-method="stop">
<property name="networkConnectorURIs">
<list>
<ref >${embed.broker.networkConnectorURI}</ref>
</list>
</property>
<property name="transportConnectorURIs">
<list>
<value>${embed.broker.transportConnectorURI}</value>
</list>
</property>
<property name="brokerName" value="embed-broker" />
</bean>
When I deploy this in an environment where the place holders are missing, Tomcat throws "Could not resolve placeholder 'embed.broker.networkConnectorURI' " error. In other words, the default values are not being picked up.
Any help would be appreciated.
To have some default values, go on this way:
<bean id="myServer" class="com.gordondickens.myapp.MyServerConfig">
<property name="serverName" value="${server.name?localhost}" />
<property name="serverPort" value="${server.port?25}" />
</bean>
Use a PropertyOverrideConfigurer instead of a PropertyPlaceholderConfigurer. That way your defaults specified in the context file will be used if no overriding property file entries are found.

Resources