Skip to content

Commit

Permalink
fix issue nutzam#312
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Sep 1, 2012
1 parent 868e9a2 commit b8f9a42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/org/nutz/dao/impl/entity/AnnotationEntityMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.nutz.lang.segment.CharSegment;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.trans.Trans;

/**
* 根据一个 Class 对象生成 Entity 的实例
Expand Down Expand Up @@ -477,7 +478,7 @@ private void _evalEntityIndexes(NutEntity<?> en, TableIndexes indexes) {
private void _checkupEntityFieldsWithDatabase(NutEntity<?> en) {
Connection conn = null;
try {
conn = datasource.getConnection();
conn = Trans.getConnectionAuto(datasource);
expert.setupEntityField(conn, en);
}
catch (Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion src/org/nutz/dao/jdbc/Jdbcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.nutz.lang.meta.Email;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.trans.Trans;

/**
* 提供一些与 JDBC 有关的帮助函数
Expand Down Expand Up @@ -85,7 +86,7 @@ public abstract class Jdbcs {
public static JdbcExpert getExpert(DataSource ds) {
Connection conn = null;
try {
conn = ds.getConnection();
conn = Trans.getConnectionAuto(ds);
DatabaseMetaData meta = conn.getMetaData();
String pnm = meta.getDatabaseProductName();
String ver = meta.getDatabaseProductVersion();
Expand Down
16 changes: 16 additions & 0 deletions src/org/nutz/trans/Trans.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.nutz.trans;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
Expand Down Expand Up @@ -206,4 +209,17 @@ public static void rollback() throws Exception {
public static void close() throws Exception {
Trans._depose();
}

/**
* 如果在事务中,则返回事务的连接,否则直接从数据源取一个新的连接
* @param ds
* @return
* @throws SQLException
*/
public static Connection getConnectionAuto(DataSource ds) throws SQLException{
if (get() == null)
return ds.getConnection();
else
return get().getConnection(ds);
}
}

0 comments on commit b8f9a42

Please sign in to comment.