Java 对事务的Spring动态语言支持

Java 对事务的Spring动态语言支持,java,spring,groovy,Java,Spring,Groovy,在我正在开发的当前应用程序中,我们觉得需要动态地重新评估一些DAO逻辑,而不需要新的部署。 为此,我选择尝试SpringGroovy集成。 我已经连接了bean,在Groovy脚本中,我可以获得javax.sql.Datasource对象的句柄。 但是当事务性操作发生时,我会遇到一个问题,例如,如果我在某个java风格的DAO中插入一行,那么该行在groovy风格的DAO中不可见,即使它是从标记为@transactional的同一服务调用的。 如果需要任何其他详细信息,我将尝试提供一些相关的代码

在我正在开发的当前应用程序中,我们觉得需要动态地重新评估一些DAO逻辑,而不需要新的部署。 为此,我选择尝试SpringGroovy集成。 我已经连接了bean,在Groovy脚本中,我可以获得javax.sql.Datasource对象的句柄。 但是当事务性操作发生时,我会遇到一个问题,例如,如果我在某个java风格的DAO中插入一行,那么该行在groovy风格的DAO中不可见,即使它是从标记为
@transactional
的同一服务调用的。 如果需要任何其他详细信息,我将尝试提供一些相关的代码

public class App {
public static void main(String[] args) {
    final AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(BaseConfig.class);

    TestService ts = ctx.getBean(TestService.class);
    ts.testGroovy();

}
}

java DAO:

@Repository
public class HibernateDAOImpl implements HibernateDAO{

@Autowired
private DataSource dataSource;


@Override
public void makeInsert() {
    Connection connection = DataSourceUtils.getConnection(dataSource);
    try {
        PreparedStatement ps = connection.prepareStatement("insert into GROOVY_TEST values(?,?)");
        ps.setLong(1, System.currentTimeMillis());
        ps.setString(2, UUID.randomUUID().toString());
        ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

用于代理的接口:

public interface Groovy {
   void testInjection();
}
为了在Groovy中注入数据源,我扩展了一个自动连接数据源实例的java类

public abstract class GroovyHelper implements Groovy{

 @Autowired 
 private DataSource dataSource;

 public DataSource getDataSource() {
    return dataSource;
 }  
}
groovy文件:

import ro.asf.groovy.GroovyHelper
import javax.sql.DataSource
import groovy.sql.Sql

class GroovyImpl extends GroovyHelper {

void testInjection() {
    //throw new RuntimeException("error")
    Sql sql = new Sql(dataSource)
    sql.eachRow('''SELECT * FROM GROOVY_TEST ''', { article ->
        println article.value
    })
}    
}
以及将groovy脚本连接到spring的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">

    <lang:groovy id="groovy" script-source="file:scripts/Groovy.groovy" />

</beans>

在verify DAO层中,插入按预期进行

谢谢,

丹尼尔

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">

    <lang:groovy id="groovy" script-source="file:scripts/Groovy.groovy" />

</beans>