Hibernate 不提交数据
我正在尝试编写一个 Web 服务来使用 jBoss 4.2.3 和 Oracle 存储一些数据。
我想将 Hibernate 与 CMT 结合使用,在我看来它不会提交事务。 我有以下代码:
Bean:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ZzzBean implements ZzzI {
public ZzzBean() {
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public int addZzz() {
try {
Zzz z = new Zzz();
z.setA("a");
z.setI(new BigDecimal(11));
HibernateUtil.getSessionFactory().getCurrentSession().save(z);
} catch (Exception ex) {
ex.printStackTrace();
}
return 1;
}
}
接口:
@Local
public interface ZzzI {
int addZzz();
}
我的会话工厂类:
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Configuration configuration;
static {
try {
configuration = new AnnotationConfiguration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.cfg.xml具有以下内容:
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@//my_host</property>
<property name="hibernate.connection.username">my_user</property>
<property name="hibernate.connection.password">my_pass</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">jta</property>
<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<mapping resource="com/example/Zzz.hbm.xml" />
</session-factory>
映射< /strong> 文件:
<hibernate-mapping>
<class name="com.example.Zzz" table="ZZZ">
<id name="i" type="big_decimal">
<column name="i" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="a" type="string">
<column name="a" length="20" />
</property>
</class>
</hibernate-mapping>
Entity 类:
@Entity
@Table(name = "ZZZ")
public class Zzz implements Serializable {
private static final long serialVersionUID = -4165930294512113400L;
private BigDecimal i;
private String a;
public Zzz(){}
@Id
@Column(name = "i", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getI() {
return this.i;
}
@Column(name = "a", length = 20)
public String getA() {
return this.a;
}
public void setI(BigDecimal i) {
this.i = i;
}
public void setA(String a) {
this.a = a;
}
}
和 Web 服务代码:
@WebService(name = "Zzz", serviceName = "Zzz")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@Stateless
public class ZzzWS {
@EJB(beanName = "ZzzBean")
ZzzI z;
@WebMethod
public int addZzz() {
return z.addVisit();
}
}
当我调用 Web 服务方法时,我有以下输出:
13:43:52,671 INFO [Version] Hibernate Annotations 3.2.1.GA
13:43:52,682 INFO [Environment] Hibernate 3.2.4.sp1
13:43:52,686 INFO [Environment] hibernate.properties not found
13:43:52,687 INFO [Environment] Bytecode provider name : javassist
13:43:52,691 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
13:43:52,753 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
13:43:52,753 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
13:43:52,869 INFO [Configuration] Reading mappings from resource : com/example/Zzz.hbm.xml
13:43:52,884 INFO [Configuration] Configured SessionFactory: null
13:43:53,022 INFO [HbmBinder] Mapping class: com.example.Zzz -> ZZZ
13:43:53,124 INFO [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
13:43:53,124 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 20
13:43:53,124 INFO [DriverManagerConnectionProvider] autocommit mode: false
13:43:53,168 INFO [DriverManagerConnectionProvider] using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@//my_host
13:43:53,168 INFO [DriverManagerConnectionProvider] connection properties: {user=***, password=***}
13:43:53,739 INFO [SettingsFactory] RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
13:43:53,739 INFO [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 11.2.0.2.0
13:43:53,754 INFO [Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
13:43:53,758 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.CMTTransactionFactory
13:43:53,760 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
13:43:53,761 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
13:43:53,761 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
13:43:53,761 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
13:43:53,762 INFO [SettingsFactory] JDBC batch size: 15
13:43:53,762 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
13:43:53,762 INFO [SettingsFactory] Scrollable result sets: enabled
13:43:53,762 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
13:43:53,762 INFO [SettingsFactory] Connection release mode: auto
13:43:53,763 INFO [SettingsFactory] Default batch fetch size: 1
13:43:53,763 INFO [SettingsFactory] Generate SQL with comments: disabled
13:43:53,763 INFO [SettingsFactory] Order SQL updates by primary key: disabled
13:43:53,763 INFO [SettingsFactory] Order SQL inserts for batching: disabled
13:43:53,763 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:43:53,765 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
13:43:53,765 INFO [SettingsFactory] Query language substitutions: {}
13:43:53,765 INFO [SettingsFactory] JPA-QL strict compliance: disabled
13:43:53,765 INFO [SettingsFactory] Second-level cache: enabled
13:43:53,765 INFO [SettingsFactory] Query cache: disabled
13:43:53,765 INFO [SettingsFactory] Cache provider: org.hibernate.cache.NoCacheProvider
13:43:53,765 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
13:43:53,765 INFO [SettingsFactory] Structured second-level cache entries: disabled
13:43:53,769 INFO [SettingsFactory] Echoing all SQL to stdout
13:43:53,769 INFO [SettingsFactory] Statistics: disabled
13:43:53,769 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
13:43:53,769 INFO [SettingsFactory] Default entity-mode: pojo
13:43:53,769 INFO [SettingsFactory] Named query checking : enabled
13:43:53,789 INFO [SessionFactoryImpl] building session factory
13:43:54,121 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
13:43:54,122 INFO [NamingHelper] JNDI InitialContext properties:{}
13:43:54,174 INFO [STDOUT] Hibernate: insert into ZZZ (a, i) values (?, ?)
但数据库没有新记录,即使我阻止jBoss。我的错在哪里?请帮忙。
PS 当我不使用 CMT 并编写如下代码时:
tx = session.beginTransaction();
session.persist(...);
tx.commit();
一切正常。
I'm trying to write a web service to store some data using jBoss 4.2.3 and Oracle.
I suppose to use Hibernate with CMT and seems to me it doesn't commit the transaction.
I have the following code:
Bean:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ZzzBean implements ZzzI {
public ZzzBean() {
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public int addZzz() {
try {
Zzz z = new Zzz();
z.setA("a");
z.setI(new BigDecimal(11));
HibernateUtil.getSessionFactory().getCurrentSession().save(z);
} catch (Exception ex) {
ex.printStackTrace();
}
return 1;
}
}
Interface:
@Local
public interface ZzzI {
int addZzz();
}
My session factory class:
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Configuration configuration;
static {
try {
configuration = new AnnotationConfiguration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.cfg.xml has following:
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@//my_host</property>
<property name="hibernate.connection.username">my_user</property>
<property name="hibernate.connection.password">my_pass</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">jta</property>
<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<mapping resource="com/example/Zzz.hbm.xml" />
</session-factory>
Mapping file:
<hibernate-mapping>
<class name="com.example.Zzz" table="ZZZ">
<id name="i" type="big_decimal">
<column name="i" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="a" type="string">
<column name="a" length="20" />
</property>
</class>
</hibernate-mapping>
Entity class:
@Entity
@Table(name = "ZZZ")
public class Zzz implements Serializable {
private static final long serialVersionUID = -4165930294512113400L;
private BigDecimal i;
private String a;
public Zzz(){}
@Id
@Column(name = "i", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getI() {
return this.i;
}
@Column(name = "a", length = 20)
public String getA() {
return this.a;
}
public void setI(BigDecimal i) {
this.i = i;
}
public void setA(String a) {
this.a = a;
}
}
And the web service code:
@WebService(name = "Zzz", serviceName = "Zzz")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@Stateless
public class ZzzWS {
@EJB(beanName = "ZzzBean")
ZzzI z;
@WebMethod
public int addZzz() {
return z.addVisit();
}
}
When I call web service method I have the following output:
13:43:52,671 INFO [Version] Hibernate Annotations 3.2.1.GA
13:43:52,682 INFO [Environment] Hibernate 3.2.4.sp1
13:43:52,686 INFO [Environment] hibernate.properties not found
13:43:52,687 INFO [Environment] Bytecode provider name : javassist
13:43:52,691 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
13:43:52,753 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
13:43:52,753 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
13:43:52,869 INFO [Configuration] Reading mappings from resource : com/example/Zzz.hbm.xml
13:43:52,884 INFO [Configuration] Configured SessionFactory: null
13:43:53,022 INFO [HbmBinder] Mapping class: com.example.Zzz -> ZZZ
13:43:53,124 INFO [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
13:43:53,124 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 20
13:43:53,124 INFO [DriverManagerConnectionProvider] autocommit mode: false
13:43:53,168 INFO [DriverManagerConnectionProvider] using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@//my_host
13:43:53,168 INFO [DriverManagerConnectionProvider] connection properties: {user=***, password=***}
13:43:53,739 INFO [SettingsFactory] RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
13:43:53,739 INFO [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 11.2.0.2.0
13:43:53,754 INFO [Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
13:43:53,758 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.CMTTransactionFactory
13:43:53,760 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
13:43:53,761 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
13:43:53,761 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
13:43:53,761 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
13:43:53,762 INFO [SettingsFactory] JDBC batch size: 15
13:43:53,762 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
13:43:53,762 INFO [SettingsFactory] Scrollable result sets: enabled
13:43:53,762 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
13:43:53,762 INFO [SettingsFactory] Connection release mode: auto
13:43:53,763 INFO [SettingsFactory] Default batch fetch size: 1
13:43:53,763 INFO [SettingsFactory] Generate SQL with comments: disabled
13:43:53,763 INFO [SettingsFactory] Order SQL updates by primary key: disabled
13:43:53,763 INFO [SettingsFactory] Order SQL inserts for batching: disabled
13:43:53,763 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:43:53,765 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
13:43:53,765 INFO [SettingsFactory] Query language substitutions: {}
13:43:53,765 INFO [SettingsFactory] JPA-QL strict compliance: disabled
13:43:53,765 INFO [SettingsFactory] Second-level cache: enabled
13:43:53,765 INFO [SettingsFactory] Query cache: disabled
13:43:53,765 INFO [SettingsFactory] Cache provider: org.hibernate.cache.NoCacheProvider
13:43:53,765 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
13:43:53,765 INFO [SettingsFactory] Structured second-level cache entries: disabled
13:43:53,769 INFO [SettingsFactory] Echoing all SQL to stdout
13:43:53,769 INFO [SettingsFactory] Statistics: disabled
13:43:53,769 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
13:43:53,769 INFO [SettingsFactory] Default entity-mode: pojo
13:43:53,769 INFO [SettingsFactory] Named query checking : enabled
13:43:53,789 INFO [SessionFactoryImpl] building session factory
13:43:54,121 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
13:43:54,122 INFO [NamingHelper] JNDI InitialContext properties:{}
13:43:54,174 INFO [STDOUT] Hibernate: insert into ZZZ (a, i) values (?, ?)
But database does not have a new record even when I stop jBoss. Where is my fault? Please help.
P.S. When I am not using CMT and write code like:
tx = session.beginTransaction();
session.persist(...);
tx.commit();
all works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案:我将数据库连接配置从 hibernate.cfg.xml 移动到数据源(Z-ds.xml),现在它可以工作了。当然,这不是问题的正常解决方案,而是一种解决方法。感谢大家!
I have found a solution: I moved database connection configuration from hibernate.cfg.xml to datasource (Z-ds.xml) and it works now. Of course this is not a normal solution of a problem, but a workaround. Thanks to all!