Hibernate SessionFactory bean without Spring - spring

Due to some GAE limitations, I cannot use the Spring session factory.
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>it.trew.prove.model.beans.Scadenza</value>
<value>it.trew.prove.model.beans.Fornitore</value>
<value>it.trew.prove.model.beans.Societa</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!-- <prop key="hibernate.hbm2ddl.import_files">/setup.sql</prop> -->
</props>
</property>
</bean>
See my other question if interested in it: Spring Autowiring stopped working on GAE
Now I want to create a session factory without org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
How can I configure the pure session factory bean, using only hibernate stuff?

Why are you wanting to use hibernate with GAE? Are you using CloudSQL or the datastore? AppEngine isn't really meant for hibernate type stuff... you're much better off looking at using Objectify or Twig, or if you want an ORM solution, JDO.

Related

What is the default connection pool of LocalContainerEntityManagerFactoryBean

We have a spring-based web app which uses spring-data-jpa and openjpa with PostgreSQL DB. The EntityManager settings as below.
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter" />
</property>
<property name="persistenceUnitName" value="PersistenceUnit" />
<property name="jpaProperties">
<props>
<prop key="openjpa.ConnectionURL">jdbc:postgresql://${db.host}:${db.port}/${db.database}</prop>
<prop key="openjpa.ConnectionUserName">${db.username}</prop>
<prop key="openjpa.ConnectionPassword">${db.password}</prop>
<prop key="openjpa.ConnectionDriverName">org.postgresql.Driver</prop>
</props>
</property>
</bean>
As we have given the jpaProperties and org.postgreql.Drive only, the web app is running on tomcat8. What is the default connection pool will be used for this case?
org.postgresql.ds.PGPoolingDataSource
org.apache.commons.dbcp.BasicDataSource
com.mchange.v2.c3p0.ComboPooledDataSource
org.apache.tomcat.jdbc.pool
If we want to give a dataSource bean in this case, which connection pool is the best practice?

What's the difference between mail.smtp.auth and mail.smtp.requiresAuthentication in Javamail/Spring

I'm using Spring 3.1.0 to send email using the JavaMailSenderImpl class.
Here is my Spring config xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="your.mail.server"/>
<property name="port" value="25"/>
<property name="protocol" value="smtp"/>
<property name="username" value=""/>
<property name="password" value=""/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.connectiontimeout">5000</prop>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.userset">true</prop>
<prop key="mail.mime.charset">UTF-8</prop>
<prop key="mail.smtp.isSecure">false</prop>
<prop key="mail.smtp.requiresAuthentication">true</prop>
<prop key="mail.smtp.port">25</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
To be honest I'm not sure where I got all this from, probably some sample code I got from a tutorial sometime.
I want to turn off SMTP authentication and I can see from the JavaMail docs that I need to set the mail.smtp.auth property to false. But in my config xml I also set a property called mail.smtp.requiresAuthentication that from the name seems equally relevant, but I can't find any reference to it in the JavaMail or Spring documentation. Google does however find lots of examples of it being used.
Is mail.smtp.requiresAuthentication actually needed and if so what does it do? Could it be a deprecated, or even bogus, property?
You really should understand the code before you do cut&paste...
There is no mail.smtp.requiresAuthentication property in JavaMail.
I don't know anything about how Spring uses JavaMail, but in JavaMail if you don't want to do authentication, don't call the connect method that provides a username and password, or pass the username and password as null with mail.smtp.auth set to false. If you call connect with username or password not null, it doesn't matter what mail.smtp.auth is set to.

Spring 3.2 + JPA (with Hibernate 3.6) + Websphere 8 (JTA) not flushing/commiting some operations

I have some issues after changing my backend from Hibernate to JPA (+Hibernate). I am using Websphere and container transaction management through org.springframework.transaction.jta.WebSphereUowTransactionManager. Some operations don't behave as expected:
DELETE OPERATION: If I don't flush the EntityManager manually it won't issue the delete, nothing happens actually.
#Transactional
#Override
public void deleteApplication(Integer appId) {
Application app = appDAO.findOne(appId);
//em.flush(); to force the flush(), otherwise it doesn't do anything
appDAO.delete(app);
}
INSERT WITH CASCADE OPERATION: The Application entity has a N:M relation with Attribute. I try to persist an Application with some Attribute added to its Application.attributes List. Right after the appDAO.save() I see a insert into Application sentence. However, there are never any inserts for the cascaded Attributes into the join table. Again, I need to manually flush() the em to issue de sql statements left.
#Transactional
#Override
public Application createApplication(Application application) {
appDAO.save(application);
//em.flush(); Needed to force the cascade into the join table
return application
}
I have tried changing the transactionManager for a non-container-managed one (org.springframework.orm.jpa.JpaTransactionManager) and it works perfectly without needing to use manual flush.
I am not using the persistence.xml file, following the approach introduced in Spring 3.1 (jtaDataSource + packagesToScan). However I have also tried with the traditional config with a persistence.xml file and I experienced the same wrong behaviour.
¿Any suggestions?
My setup:
<bean id="mainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="mainPersistenceUnit"/>
<property name="jtaDataSource" ref="mainDataSource"/>
<property name="packagesToScan" ref="packages-mainEntityManagerFactory"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
</bean>
<tx:annotation-driven order="0" />
<!-- Drives transactions using local JPA APIs -->
<bean name="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
In case someone has the same problem. The solution comes down to using
<prop key="hibernate.transaction.factory_class">org.hibernate.ejb.transaction.JoinableCMTTransactionFactory</prop>
instead of
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>

Mapping a url to one servlet

I am using the class org.springframework.web.servlet.mvc.ServletWrappingController to wrap a servlet, that we need to process one http request in Spring MVC, but reference spring mvc version 3.2 guide says:
There are also several things no longer possible:
Select a controller first with a SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping
So then this is not possible any longer, as the api says:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
<property name="mappings">
<props>
<prop key="*.do">strutsWrappingController</prop>
</props>
</property>
</bean>
<bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController">
<property name="servletClass">
<value>org.apache.struts.action.ActionServlet</value>
</property>
<property name="servletName">
<value>action</value>
</property>
<property name="initParameters">
<props>
<prop key="config">/WEB-INF/struts-config.xml</prop>
</props>
</property>
</bean>
how can i map a url to a ServletWrappingController bean?
Thanks in advance.

how to flush hibernate session using spring transaction manager for non read only methods?

I've read several topics but still don't understand. I have a spring mvc application that use hibernate on a DAO layer. The sessionFactory and transaction manager was configured in a standard way, service layer use `#Transaction` Also I use `OpenSessionInViewFilter`. I know that this filter set session flush mode to NEVER but then tx manager set it to AUTO for each tx and return it back once tx is commited. Also I get session as `sessionFactory.getCurrentSession()`.
My problem is with the non-read-only methods. Objects do not appears in the database after method is finished. Only after I explicitly call session.flush() the session state synchronized with the DB. As for me it's not a normal way. I think there is should be a property for tx manager or maybe sessionFactory or another bean that make a auto commit for non-read-only methods.
So, why FlushMode.AUTO may not work? Is it normal to call session.flush() manually in each non-read-only methods?
<bean name="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="aimsDataSource">
<property name="packagesToScan">
<list>
<value>net.adaptiveservices.aims.rc.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <!--TODO Remove in production-->
</props>
</property>
</bean>
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>

Resources